[racket] [htdp] Help with Exercise 12.4.2

From: David Yrueta (dyrueta at gmail.com)
Date: Tue Jul 6 18:46:33 EDT 2010

my base case the function should return a
list of words but before returned a single word.

Very good decision.  Now take a close look at how you phrased the second
condition.  According to the design recipe, it should be structured to match
the second condition of your data definition for list-of-words.  Does it?

(define (insert-everywhere/in-single-word s w)
   [(empty? w) (list s)]
   *[else  (list (list s (first w)) (cons (first w)
(insert-everywhere/in-single-word s (rest w))))]))*

On Tue, Jul 6, 2010 at 3:14 PM, Sam Griff <sgriff89 at gmail.com> wrote:

> > Exercise 12.4.2 is notorius. When looking to (list 'a 'b 'c), you should
> > exspect 6 distinct permutations.
> > Jos
> Thanks for your response but that really doesn't help me much. I'm
> focusing on insert-everywhere/in-single-word now. I understand what
> arrangements does
> and what permutations to expect.
> > My first suggestion is to check the function examples to make sure the
> values they expect are consistent with your data definitions.
> Can you explain please? I'm not sure I fully understand. The only
> thing I could see was in my base case the function should return a
> list of words but before returned a single word. Is that what you
> mean?
> (define (insert-everywhere/in-single-word s w)
>  (cond
>     [(empty? w) (cons (cons s empty) empty)]
>    [else ... (first w) ... (insert-everywhere/in-single-word s (rest w))]))
> > Amen.
> >
> > -- Make sure you truly understand list vs cons. If you have any doubts,
> > stick to cons.-- For your template, spell out what each expression
> produces
> > before you finish the definition.-- Use the examples, especially the ones
> > that fail.-- Figure out what you need to combine the pieces.-- Ideally,
> > arrange the thing in a table:
> >  input | (first input) | .... | recursive result | expected result
> >
> > and keep adding examples until it clicks: i.e., until you know how to
> > combine recursive result with some other things to get the expected
> result.
> >
> > And do stick to the design recipe all the way down.
> Thanks! I will try those suggestions and see if they help.
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20100706/b300c431/attachment.html>

Posted on the users mailing list.