Fwd: [plt-scheme] HtDP newbie question, 12.4.2

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

Dear All,

For those of you that have been wondering about what I wrote I forward
my message to Cooke below. My apologies for mistakenly "replying"
instead of "replying to all."

Cheers,

Marco

---------- Forwarded message ----------
From: Marco Morazan <morazanm at gmail.com>
Date: Fri, Mar 28, 2008 at 2:14 PM
Subject: Re: [plt-scheme] HtDP newbie question, 12.4.2
To: Cooke Kelsey <cookekelsey at yahoo.com>


Dear Cooke,

I have been following this discussion with interest. It is giving me
insight into where people get stuck. I decided to sit down and code
this for myself to see if my mental processes were clear enough to
design and implement the solution.

Here are some thoughts I went through. It is different from the create
a table approach, but maybe it gives you some additional insight in
conjunction with the tables.

We are given:

(define (arrangements a-word)
 (cond [(null? a-word) '(())]
       [else (insert-everywhere-in-all-words (first a-word)
                                             (arrangements (rest a-word)))]))

;; insert-everywhere-in-all-words: letter list-of-words -> list-of-words
;; purpose: create all words possible from letter and word-list
(define (insert-everywhere-in-all-words letter word-list)
;;<use template for function on word-list>
(cond [(null? word-list) <the-right-value-when-word-list-is-empty>]
       [else <create a list of words from
                   <create a list of words from letter and the (first
word-list)>
                   <creating a list of words from letter and the (rest
word-list)>
                >]))

For <creating a list of words from letter and the (rest word-list)> it
is easy to see what is needed.

For <create a list of words from letter and the (first word-list)>,
you need a function that consumes a letter and a word and that returns
all the words you can create from the letter and the word. How do you
do that? Chances are you need to insert-everywhere-in-word.

;; insert-everywhere-in-word: letter word -> list-of-words
;; purpose: create all words possible from letter and a-word
(define (insert-everywhere-in-word letter a-word)
;;<use template for function on word>

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 member of R?

I hope this helps,

Marco


Posted on the users mailing list.