[racket] [htdp] Help with Exercise 12.4.2

From: Marco Morazan (morazanm at gmail.com)
Date: Wed Jul 7 21:53:18 EDT 2010

So, is that table really useful? I am not trying to be funny here.
I've chimed in the past on this problem to the point that some website
(according to my students) has earmarked me the expert on this
problem--some honor! In any case, it seems to me that you can get to
the solution by being disciplined about using the DR *and* remembering
the rule that states that if you need to process different data of
arbitrary length then you should call an auxiliary function (instead
of making a recursive call).

You will notice that as you work your way through the problem, the
above rule tells you at each step that there is another function to
write. I know that tables like the one quoted below work for some
students, but I rarely find the use of tables particularly
enlightening. My students are a dirty wash when it comes to using and
creating such tables.

Feedback/insights welcomed.....

Marco


On Wed, Jul 7, 2010 at 2:06 PM, Sam Griff <sgriff89 at gmail.com> wrote:
>> 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.
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
>



-- 

Cheers,

Marco


Posted on the users mailing list.