[plt-scheme] HTDP Exercise 12.4.2 ... Help!

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Sun Apr 26 09:13:45 EDT 2009

On Apr 26, 2009, at 2:19 AM, S Brown wrote:

> 4. My header/contract and template for insert-everywhere/in-all-words:
> ;; insert-everywhere/in-all-words: symbol list-of-words  ->  list- 
> of-words
> ;; to create a list-of-words like a-low, but with the symbol s  
> inserted at
> ;; the beginning, end, and in-between each letter of each word in  
> the list-of-words in a-low
> (define (insert-everywhere/in-all-words s a-low)
>   (cond
>     ((empty? a-low) ... s ...)
>     (else ... s ... (first a-low) ...
>             ... (insert-everywhere/in-all-words s (rest a- 
> low)) ... )))
> ...
> Next, I looked at the first line of else, ... s ... (first a-low) ...,
> now I know that in the example above that (first a-low) becomes (list
> 'm 'e)

Good: you've filled in the value of (first a-low) for this example,  
and you're (at least implicitly) comparing it with the "right answer"  
to the whole example.  You need to also do the same with each of the  
other expressions in your template: s, and (insert-everywhere/in-all- 
words s (rest a-low)).  The former is trivial.  For the latter, write  
down what (insert-everywhere/in-all-words s (rest a-low)) SHOULD be,  
without worrying for now about how it's actually defined (since you  
haven't finished writing it yet).  In most cases, the value of the  
recursive call will be "most" of the right answer; identify what's  
missing, and figure out how to fill the gap.  If there's already a  
function that builds what you need to fill the gap, use it; if not,  
write it.

> , so all I need to do with the s is just add it to the
> beginning, and I can do that using append, so I know that line can
> become (append (list s) (first a-low)).

This part of the sentence I don't buy: this is PART of what's  
missing, but not all of what's missing.

> And now my brain melts and I have no idea what to do next. All I've
> done is add the symbol to the beginning of the word, but what about
> the rest of it? If I go to the next line, I'm just working on the next
> word in the list-of-words, so where am I ever going to do the work
> needed to put the symbol between the rest of the letters??? Am I even
> on the right track with this? I don't want to continue with HTDP
> without understanding this exercise! Can anyone help?

Yes, you're asking exactly the right questions.  The example you  
chose is perhaps a little too simple: it's only 2 words, so the  
"rest" of it is only 1 word, which could be too simple for your human  
pattern-matching hardware to kick in properly, so I would start with  
a list of 3 words.  However, the real problem is that you started  
with "what can I do with (first a-low)?" rather than "what isn't  
taken care of by the recursive call?"

My CS1 students right now are struggling with this problem and the  
related problem of finding all the subsets of a given list  
(regardless of order), only they have to do it in Java, which is  

Stephen Bloch
sbloch at adelphi.edu

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090426/4bf31ab1/attachment.html>

Posted on the users mailing list.