[plt-scheme] HtDP newbie question, 12.4.2

From: Cooke Kelsey (cookekelsey at yahoo.com)
Date: Thu Mar 27 13:20:53 EDT 2008

Dear Dr. Felleisen, 
  thanks so much for responding!  Your suggestions really helped me to think about the problem.
  Here's a table of examples, as you suggested:
; s | low              |(first..)|(rest..)    | result
; X | '(AT)            |'(AT)    |empty       | '((XAT) (AXT) (ATX))
; X | '((AB) (CD) (EF))|'(AB)    |'((CD) (EF))| '((XAB) (AXB) (ABX) (XCD) (CXD) (CDX) (XEF) (EXF) (EFX))

  Here's an attempt to "go from the first columns to the last":  
; Definitions:
(define (insert-everywhere/in-all-words s low)
    [(empty? (rest low)) empty]
    [else (append (insert-in-single-word s (first low)) (insert-everywhere/in-all-words s (rest low)))]))
  (define (insert-in-single-word s word)
    [(empty? (rest word)) empty]
    [else (append (list (first word) s (rest word)) (insert-in-single-word s (rest word)))]))

  Unfortunately, the list of words gets shorter and shorter (sATX, sTX, sX).  I tried to think of a way to add a recursive "prefix" that gets longer, but each time the function cycles the first letters seem to disappear into the void...
Matthias Felleisen <matthias at ccs.neu.edu> wrote:
  On Mar 25, 2008, at 1:48 PM, Cooke Kelsey wrote:    I am trying to learn HtDP by myself, and like Dave Yrueta I am stumped by 12.4.2. 

[Oops I forgot to reply to his last message.]

    ; Purpose: to insert a symbol between every letter of every word, e.g. X, AT-->XAT, AXT, ATX.

  Please reformulate the example in terms of symbols and lists. 

     ;Function Template: 
  (define (insert-everywhere/in-all-words s low)
    [(empty? (rest low))...]
    [else ...(first low)...insert-everywhere/in-all-words s (rest low)]))

  It is suggested to use "append", which is easy one or two times---(append x word) or (append (append (first word) x) (rest word))----but I can't see how to use this recursively for an arbitrary number of letters and words. 

  So far so good. Let's try what HtDP/2e will introduce: 

  Please make a table of the following shape: 

   s   |   low  |   (first low)  |  (insert-everywhere/in-all-words s (rest low) | expected result for (insert-... s low)
   X   |   AT   |     ?             |  use the purpose statement to determine this |   (list `XAT' `AXT' `ATX')


  As I said, you need to reformulate this in terms of symbols and lists. If it doesn't jump out at you how to go from the four columns to the last one, then make a couple of more examples. I am pretty sure it will. Here is the rule of thumb: 

   --> you know of a built-in function/primitive that does the work 
   --> you don't. in this case, you make a wish: a purpose statement and a contract for a helper function that does it for you. 

  You may also have to combine a primitives with functions. 

  Please report back -- Matthias






  The fact that the "hint" refers to the keyword list, which the book hasn't covered yet, makes me wonder if it's even possible to answer this problem, given what we know so far...
  Thanks very much, 

  Looking for last minute shopping deals? Find them fast with Yahoo! Search.  _________________________________________________
    For list-related administrative tasks:

Looking for last minute shopping deals?  Find them fast with Yahoo! Search.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080327/7339a263/attachment.html>

Posted on the users mailing list.