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

From: Stephen (ontheheap at gmail.com)
Date: Fri May 1 00:54:30 EDT 2009

I'm still trying to understand exactly how this helps me:

(cond ((empty? a-word) (list (list a-letter)))
                 ; a-letter                           symbol
                 ; a-word                           los
         (list 'a 'b 'c 'd)
                 ; (first a-word)                 symbol
                 ; (rest a-word)                 los
    (list 'b 'c 'd)
                 ; (insert-everywhere a-letter (rest a-word))    lolos (list
(list 'x 'b 'c 'd) (list 'b 'x 'c 'd) (list 'b 'c 'x 'd) (list 'b 'c 'd 'x))
                 ; right answer                 lolos             (list
(list 'x 'a 'b 'c 'd) (list 'a 'x 'b 'c 'd) (list 'a 'b 'x 'c 'd) (list 'a
'b 'c 'x 'd) (list 'a 'b 'c ' d 'x))

I see how when (rest a-word) is (list 'b 'c d), that (insert-everywhere
a-letter (rest a-word)) is (list (list 'x 'b 'c 'd) (list 'b 'x 'c 'd)
etc...), because this is what I've defined the function insert-everywhere to
do. I think I understand what the next step is now, it's figuring out how to
go from (list (list 'x 'b 'c 'd) etc...) to the right answer which is (list
(list 'x 'a 'b 'c 'd) etc.

So, I have the following information/data available to me in order to do

                 ; a-letter                           symbol
                 ; a-word                           los
         (list 'a 'b 'c 'd)
                 ; (first a-word)                 symbol
                 ; (rest a-word)                 los
    (list 'b 'c 'd)

Am I thinking about this correctly?

At this point, it looks like in order to turn this:

(list (list 'x 'b 'c 'd)
      (list 'b 'x 'c 'd)
      (list 'b 'c 'x 'd)
      (list 'b 'c 'd 'x))

into this:

(list (list 'x 'a 'b 'c 'd)
      (list 'a 'x 'b 'c 'd)
      (list 'a 'b 'x 'c 'd)
      (list 'a 'b 'c 'x 'd)
      (list 'a 'b 'c ' d 'x))

I have to
1. insert (first a-word) between 'x and 'b for the first list
2. insert (first a-word) in front of the rest of the lists

So, I potentially need some function that can prepend a symbol onto each
word in a list of words.

; prepend-letter: Letter List-Of-Words  ->  List-Of-Words
; creates a List-Of-Words where a-letter is inserted before
; each word in a-list-of-words
(define (prepend-letter a-letter a-list-of-words )
    ((empty? a-list-of-words) empty)
    (else (append (list (append (list a-letter) (first a-list-of-words)))
                  (prepend-letter a-letter (rest a-list-of-words))))))

is this prepend-letter function one of the helper functions I need? I'm
starting to think I might be on the right track here. Completing the
insert-everywhere function still eludes me, but maybe the helper-functions
need to be written first before insert-everywhere will work correctly?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090501/a61e6ac2/attachment.html>

Posted on the users mailing list.