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

From: S Brown (ontheheap at gmail.com)
Date: Sun Apr 26 02:19:53 EDT 2009

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, 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 'h (cons 'i empty))
                (cons 'h (cons 'i empty))
                (cons 'b (cons 'o (cons 'b empty)))

4. My header/contract and template for insert-everywhere/in-all-words:

;; insert-everywhere/in-all-words: symbol list-of-words  ->  list-of-
;; 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)
    ((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?

Posted on the users mailing list.