[plt-scheme] HtDP newbie question, 12.4.2

From: Marco Morazan (morazanm at gmail.com)
Date: Fri Mar 28 20:37:01 EDT 2008

Dear Cooke,

>  (2) Marco: "Here is where I believe you are really stuck. It is easy to see
>  one member of the result: (letter a-word). This puts letter before the first
>  letter of a-word. Is that the only place it can go? No, it can go before any
>  of the letters in a word and at the end of a-word. How do you insert a
>  letter everywhere in (rest a-word)? Once you have this list (call it R) all
>  you need is to do something with (first a-word).  Where does it have to go
>  in each element of  R?"
> OK, here is a function which I think answers these questions:
> (define (add-letter-to-recursive-list letter a-word)
>   (cond
>     [(empty? (rest a-word)) (cons (list letter (first a-word)) empty)]
>     [else (cons (cons letter a-word) (add-letter-to-recursive-list letter
> (rest a-word)))]))

Ah! In the face of desperation you are abandoning your knowledge!!!
Where is your contract and your purpose? Stick to them, because they
are useful!

This function consumes a letter and a word. Ask yourself: Am I
consuming a-word correctly? Does a-word always have to have a rest?
You are not using a template for a function that consumes a word! You
have, for reasons I can not fanthom, abandoned the template for a
function that consumes a word.

Bigger hint: Do you want to add a letter just to (rest a-word) or to
all the possible words that you can form with (rest a-word)? In the
latter case, what letter do you want to add to the front of each word
and from what ingridients do you want to make the list of words that a
letter will be added to?

> ;example:
> (add-letter-to-recursive-list 'x (list 'w 'a 't 'e 'r))
> ;answer
> (list
>  (list 'x 'w 'a 't 'e 'r)
>  (list 'x 'a 't 'e 'r)
>  (list 'x 't 'e 'r)
>  (list 'x 'e 'r)
>  (list 'x 'r))
> Then next question is, how can I put the missing letters back on the front
> of these words?

No! That is the wrong question in my opinion. The question is how do
you build the right words from the beginning. You seem to be trying to
fix something that is wrong instead of building correctly from the

Allow me to take license. You want to build a skyscraper. You do not
want to build something that looks like a skyscraper and then patch it
up. Does that make sense to you?

> At this point, I think my problem is too many levels of abstraction.  I
> think that you all can see the answer very clearly, but I can't visualize
> multiple recursive functions.  The tables, etc have been extremely helpful
> for me to develop the individual functions.  As far as the final solution,
> perhaps I don't have the ability to "see" it on that level of abstraction?

No, I disagree. You just need to stick to the design recipe and
exploit the templates for each type of data (not things that look like
the templates for your data). Think of each function strictly in terms
of its contract and purpose and follow the shape of your data.

I hope that helps.



Posted on the users mailing list.