[racket] [htdp] Help with Exercise 12.4.2

From: Sam Griff (sgriff89 at gmail.com)
Date: Wed Jul 7 14:06:14 EDT 2010

> Isn't that too much help?
I don't think so because I know there is a problem with the second
clause and thinking recursively. I made a few charts like you
suggested:

  s  |             w                        | (first w) |
(insert-everywhere/in-single-word s (rest w))                   |
expected result for (insert-everywhere/in-single-word s w)
-------------------------------------------------------------------------------------------------------------------------------------
 'a  |        (cons 't empty)				|    't     |
(insert-everywhere/in-single-word 'a empty)					  | (cons (cons 'a
(cons 't empty)) (cons (cons 't (cons 'a empty)) empty))
 'a  |        empty                         |    N/A    |
N/A                                                | (cons (cons 'a
empty))


 'h  |   (cons 'i (cons 't empty))          |    'i     |
(insert-everywhere/in-single-word 'h (cons 't empty))           |
(cons (cons 'h (cons 'i (cons 't empty))) (cons (cons 'i (cons 'h
(cons 't empty))) (cons (cons 'i (cons 't (cons 'h empty))) empty)))
 'h  |   (cons 't empty)                    |    't     |
(insert-everywhere/in-single-word 'h empty)                     |
(cons (cons 'h (cons 't empty)) (cons (cons 't (cons 'h empty))
empty))
 'h  |   empty								|    N/A    |                 N/A											  |
(cons (cons 'h empty) empty)

 'a  |  (cons 'r (cons 'e (cons 'd empty))) |    'r     |
(insert-everywhere/in-single-word 'a (cons 'e (cons 'd empty))) |
(cons (cons 'a (cons 'r (cons 'e (cons 'd empty)))) (cons (cons 'r
(con s 'a (cons 'e (cons 'd empty)))) (cons (cons 'r (cons 'e (cons 'a
(cons 'd empty)))) (cons (cons 'r (cons 'e (cons 'd (cons 'a empty))))
empty))))
 'a  |  (cons 'e (cond 'd empty))           |    'e     |
(insert-everywhere/in-single-word 'a (cons 'd empty))           |
(cons (cons 'a (cons 'e (cons 'd empty))) (cons (cons 'e (cons 'a
(cons 'd empty))) (cons (cons 'e (cons 'd (cons 'a empty))) empty)))
 'a  |  (cons 'd empty)					    |    'd     |
(insert-everywhere/in-single-word 'a empty)                     |
(cons (cons 'a (cons 'd empty)) (cons (cons 'd (cons 'a empty))
empty))
 'a  |   empty								|    N/A    |                 N/A											  |
(cons (cons 'a empty) empty)

And I see that I need some way of putting not only the symbol into all
the words but also keep (first word) in the list of words so it
doesn't get lost. At this point it seems obvious to me that I need
another auxiliary function to help with this.


Posted on the users mailing list.