[plt-scheme] HTDP Exercise 12.4.2 ... Help!

From: Andrei Estioco (chadestioco at gmail.com)
Date: Sun Apr 26 06:17:54 EDT 2009

On Sun, Apr 26, 2009 at 2:19 PM, S Brown <ontheheap at gmail.com> wrote:

> I've been trying to do this exercise all evening. Actually, I started
> this morning but got so frustrated that I took my dogs to the park to
> blow off some steam! I've already read through an archived discussion
> of this exercise from back in June/July of 2006 from this group, it's
> helped a little, but I don't think I would have gotten even this far
> without having found that thread. So... I'm just looking for someone
> (s) who would be nice enough to help me figure out how to solve this
> exercise using the design recipes.
> This is what I have so far:
> 1.The exercise is asking me to define the function insert-everywhere/
> in-all-words, which receives as it's arguments a symbol and a list-of-
> words, and then returns a list-of-words where the symbol has been
> placed before and after each letter in each word of the list-of-words.
> So:
> So, given:
> (insert-everywhere/in-all-words 's (list (list 'm 'e) (list 'x)))
> The result should be:
> (list (list 's 'm 'e)
>      (list 'm 's 'e)
>      (list 'm 'e 's)
>      (list 's 'e 'm)
>      (list 'e 's 'm)
>      (list 'e 'm 's)
>      (list 's 'x)
>      (list 'x 's))
> 2. The book defines a word as either:
>     1. empty, or
>     2. (cons s w), where s is a symbol ('a' thru 'z') and w is a word
>     So, some examples of words are: empty, (cons 'i empty), and (cons
> 'h (cons 'i empty))
> 3. I have defined a list-of-words as either:
>    1. empty
>    2. (cons w low), where w is a word, and low is a list of words
>   So, some examples of lists of words are:
>             (cons empty empty)
>             (cons
>                (cons 'h (cons 'i empty))
>                empty)
>             and,
>             (cons
>                (cons 'h (cons 'i empty))
>                (cons 'b (cons 'o (cons 'b empty)))
>                empty)
> 4. My header/contract and template for insert-everywhere/in-all-words:
> ;; insert-everywhere/in-all-words: symbol list-of-words  ->  list-of-
> words
> ;; to create a list-of-words like a-low, but with the symbol s
> inserted at
> ;; the beginning, end, and in-between each letter of each word in the
> list-of-words in a-low
> (define (insert-everywhere/in-all-words s a-low)
>  (cond
>    ((empty? a-low) ... s ...)
>    (else ... s ... (first a-low) ...
>            ... (insert-everywhere/in-all-words s (rest a-
> low)) ... )))
> OK, as I am understanding the design recipe, I should at this point
> start filling in each line of the cond expression.
> I started with the easy one, the empty case, which is interesting
> because (list (list)), which is a list-of-words where the only word is
> empty, is not the same thing as just plain 'ol empty. So, (empty?
> (list (list))) is false, so this empty? case is only concerned with
> (list), and in this case the correct response, I believe, is to just
> return empty.
> Next, I looked at the first line of else, ... s ... (first a-low) ...,
> now I know that in the example above that (first a-low) becomes (list
> 'm 'e), so all I need to do with the s is just add it to the
> beginning, and I can do that using append, so I know that line can
> become (append (list s) (first a-low)). Or in the case of (list
> (list)), it becomes (append 's (cons empty empty)) or just (cons 's
> empty), which I think is what I want.
> And now my brain melts and I have no idea what to do next. All I've
> done is add the symbol to the beginning of the word, but what about
> the rest of it? If I go to the next line, I'm just working on the next
> word in the list-of-words, so where am I ever going to do the work
> needed to put the symbol between the rest of the letters??? Am I even
> on the right track with this? I don't want to continue with HTDP
> without understanding this exercise! Can anyone help?
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme

I've also been through this problem and I also had a hard time filling in
the templates given by HtDP that time. And I think you are making the
mistake I did that time.

Have you tried creating helper/auxillary functions? If not then don't
hesitate to create them as described in as early as section 3. From my HtDP
experience, I've figured out that the template only provides you with the
"general structure" of the main function. It does not forbid you to create
helper functions. And if I remember properly, you're going to create a whole
legion of helper functions. But, of course, that is just an answer and not
the answer.

(When creating helper functions, I find it easier to decompose the problem
first in a pseudocode. That's when I realize what the "logical divisions" of
my task are. For each logical division, I create one helper function.)

You took your dog for a walk earlier? That's good thinking. The beginner's
mind is a fresh place to come from.

Good luck.
Chad Estioco
BS Computer Science (ongoing)
University of the Philippines-Diliman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090426/429c860b/attachment.html>

Posted on the users mailing list.