[plt-scheme] HtDP newbie question, 12.4.2

From: Cooke Kelsey (cookekelsey at yahoo.com)
Date: Thu Mar 27 15:15:28 EDT 2008

(1) At this point it becomes critical to spell out the (list ...) stuff, i.e., to turn the example into full-fledged Scheme data. Small step. 
   
  OK, here it is:
   
  ; Example: (define (insert-in-single-word 'X (list 'A 'T).  
  Result should be (list (list 'X 'A 'T) (list 'A 'X 'T) (list 'A 'T 'X))
   
  (.......which Scheme displays as (cons (cons 'X (cons 'A (cons 'T empty))) (cons (cons 'A (cons 'X (cons 'T empty))) (cons (cons 'A (cons 'T (cons 'X empty))) empty)))....)
   
  (2) I propose you make a second example again. 
   
    ;Example 2: (define (insert-in-single-word 'X (list 'Y).  
  Result should be (list (list 'X 'Y) (list 'Y 'X))
   
    (3) This is NOT a template. 
   
  Yikes, my bad.  Here's the proper template:
   
  ; Template: 
;   (define (insert-in-single-word s word)
;     (cond
;       [(empty? (rest word)) empty]
;       [else .....(first word)......(insert-in-single-word s (rest word)))]))
   
  (4) And before you complete the template build a table just like the one above for the recursive case. 
   
    First Table: 
   
   s   |   word  |   (first word)  |  (insert-in-single-word s (rest word)) | expected result for (insert-... s low)
  --------------------------------------------------------------------------------------------------------
   'X   |(list 'A 'T)|     'A          |  (insert-in-single-word s (list 'T) |   (list (list 'X 'A 'T) (list 'A 'X 'T) (list 'A 'T 'X))
   
  Table for Recursive Case:
   
     s   |   word  |   (first word)  |  (insert-in-single-word s (rest word)) | expected result for (insert-... s low)
  --------------------------------------------------------------------------------------------------------
   'X   |(list 'T)|     'T          |  (insert-in-single-word s empty |   (list (list 'X 'T) (list 'T 'X))

   
  I feel I am getting closer to the answer.  The second table suggest to me that there is something about recurring down to 2 letters and then appending the rest on.....except that I lost the rest of the letters.  
   
  The only way I can visualize it is like file cabinets, and I use a counting function to count all the letters and assigning them to little files or numbered variables and then increment the value of the input variable in between each one.  
   
  I know that is ridiculous but that is what comes to mind.



  

Matthias Felleisen <matthias at ccs.neu.edu> wrote:
  
  On Mar 27, 2008, at 2:12 PM, Cooke Kelsey wrote:
    Here's the answers to your questions:
   
  Q: What do you mean with (rest ..) ?  This doesn't look like my table. 
   
   A: Your table says, "(insert-everywhere/in-all-words s (rest low)," which means I would rewrite the whole function in the column? 
  

  I want to know what the result of (insert-everywhere/in-all-words s (rest low)) would be when low = ... 
  (Looking more closely, your second row says you did the right thing.) 
  

  

     [This isn't quite right! 'AT is NOT the representation of the English word "at".]
   
    A: I was trying to use shorthand from Intermezzo 2:

'(a b c)  This short-hand is an abbreviation for  (list 'a 'b 'c)  
  

  Then what is '(AT)? Hint: you used ' improperly, and that is common for beginners. I recommend you stick to (list 'a 'b 'c) and (list (list 'a) (list 't)) and so on. 
  

  

     Q: Why not take one step from row 1, which uses a list with one word. What would be the next case? 
   
  A: Um, two words.  I have no idea why I did that.
  

  Make up a simpler example. Simple examples are good. 
  


    Q: The second leap went wrong. Why? Is it possible that you JUMPED?  Why don't you turn the above examples into examples for this function. And *please* do provide a contract, a purpose statement. 
   
  A: OK, this is where my big problem is.  I'm not sure what you mean by JUMPED.  Can you be more specific?  I realize this is some kind of clue but I am too slow to get it. 
  

  JUMPED means you wrote the helper function w/o contract, purpose statement, template, examples. 
  

  


    
   Here's a full design recipe:
   
  ; Purpose: to insert a symbol between every letter of a single word
; Contract: symbol word --> list of words
; Example: (define (insert-in-single-word X AT).  Result should be (XAT AXT ATX)
  

  (0) The example is _conceptually_ correct (meaning at the information level). 
  

  (1) At this point it becomes critical to spell out the (list ...) stuff, i.e., to turn the example into full-fledged Scheme data. Small step. 
  (2) I propose you make a second example again. 
  


    ; Template: 
;   (define (insert-in-single-word s word)
;     (cond
;       [(empty? (rest word)) empty]
;       [else (append (list (first word) s (rest word)) (insert-in-single-word s (rest word)))]))
  

  This is NOT a template. 
  

  And before you complete the template build a table just like the one above for the recursive case. 
  

  -- Matthias
  

  

  


    

  Thanks very much.

Matthias Felleisen <matthias at ccs.neu.edu> wrote:
  
  On Mar 27, 2008, at 1:20 PM, Cooke Kelsey wrote:
    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:
   
  ;;Examples 
; s | low              |(first..)|(rest..)    | result
  

  Q: What do you mean with (rest ..) ?  This doesn't look like my table. 
  


    ;--------------------------------------------------------------------------------------------------------
; X | '(AT)            |'(AT)    |empty       | '((XAT) (AXT) (ATX))
  

  

  [This isn't quite right! 'AT is NOT the representation of the English word "at".]
  


    ; X | '((AB) (CD) (EF))|'(AB)    |'((CD) (EF))| '((XAB) (AXB) (ABX) (XCD) (CXD) (CDX) (XEF) (EXF) (EFX))
  

This second row looks hyper-complicated. 
  

  Q: Why not take one step from row 1, which uses a list with one word. What would be the next case? 
  

    

  


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

  Good the first leap went fine. 
  


     (define (insert-in-single-word s word)
    (cond
    [(empty? (rest word)) empty]
    [else (append (list (first word) s (rest word)) (insert-in-single-word s (rest word)))]))
  

  The second leap went wrong. Why? 
  

  Is it possible that you JUMPED?  Why don't you turn the above examples into examples for this function. And *please* do provide a contract, a purpose statement. 
  

  -- Matthias
  

  


    

  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)
  (cond
    [(empty? (rest low))...]
    [else ...(first low)...insert-everywhere/in-all-words s (rest low)]))

  Note: 
  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, 
   
  Cooke
  

  
---------------------------------
  Looking for last minute shopping deals? Find them fast with Yahoo! Search.   _________________________________________________
    For list-related administrative tasks:
    http://list.cs.brown.edu/mailman/listinfo/plt-scheme



  

  
---------------------------------
  Looking for last minute shopping deals? Find them fast with Yahoo! Search.


  

  
---------------------------------
  Never miss a thing. Make Yahoo your homepage.



       
---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile.  Try it now.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080327/09a50281/attachment.html>

Posted on the users mailing list.