[plt-scheme] HtDP newbie question, 12.4.2

From: Cooke Kelsey (cookekelsey at yahoo.com)
Date: Thu Mar 27 18:22:03 EDT 2008

Hi, I've made some progress but no jackpot yet...
   
  Here are the tables with all of the values, as you suggested:
   
  First Table: 
   
   s   |   word  |   (first word)  |  (insert-in-single-word s (rest word)) | expected result for (insert-... s low)
  --------------------------------------------------------------------------------------------------------
   'X   |(list 'A 'T)|     'A          |  (list (list 'X 'T) (list 'T 'X)) |   (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          |  (list 'X) |   (list (list 'X 'T) (list 'T 'X))

   
  After examining the tables, I tried a couple of new ways to get the results in the last column.  
   
  The easiest way was simply to append the letters in order, so that X, AT-->XAT, AXT ATX 
   
  (define (insert-in-single-word2 s word)
(list (append (list s) word) (append word (list s)) (append (list (first word)) (list s) (rest word))))
   
  This doesn't have any recursive part, so it only works on 3 letter words.
   
  When I tried to insert a recursive part, no matter how I arranged the parts, I always lost letters.  
   
  For instance, the definition below results in: X,AT-->XAT, ATX, X
   
  (define (insert-in-single-word s word)
  (cond
    [(empty? (rest word)) empty]
    [else (list (append (list s) word) (append word (list s)) (append (list s) (insert-in-single-word s (rest word))))]))

  

Matthias Felleisen <matthias at ccs.neu.edu> wrote:
    
On Mar 27, 2008, at 3:15 PM, Cooke Kelsey wrote:

> (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.
>
> (.......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)))....)

I recommend raising the language level to Beginner with List ..


> (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))


What is the VALUE of (insert-in-single-word s (list 'T) ) ? The 
purpose statement tells you!
   
  

> 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))

What is the VALUE of (insert-in-single-word 'X empty) ? The purpose 
statement tells you!
   
  
> 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.

Almost! You need to actually determine the value of these expressions 
not just write down the expressions to make the leap. You're almost 
there.

And don't forget: if the leap is to large, you may need yet another 
helper function.

-- Matthias






       
---------------------------------
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/2821bdff/attachment.html>

Posted on the users mailing list.