[plt-scheme] HTDP 12.4.2

From: wooks wooks (wookiz at hotmail.com)
Date: Sat Jun 24 18:43:53 EDT 2006

;CONTRACT
;prepend : symbol list-of-words -> list-of-words
;Generates a new list by prefixing each word in the list with symbol
;TEMPLATE
;(define (prepend symbol alow)
;   (cond
;      [(empty? alow) ...]
;      [else ... symbol ...(first alow).... (rest alow))]
;     ))
;
(define (prepend sym alow)
  (cond
    [(empty? alow) empty]
    [else (cons (cons sym (first alow))
                (prepend sym (rest alow)))
          ]
    )
  )
;TESTS
(prepend 'g (list
             (list 'd 'a 'r 'k)
             (list)
             (list 'x 'g)
             (list 'p)
             (list 'j 'k 'l)
             )
         )


;CONTRACT
;interperse : symbol word - list-of-words
; produces a list of words by interpersing symbol into each position in word 
(including before and after)
;

;
;TEMPLATE
;(define (interperse sym word)
;   (cond
;      [(empty? word) ....]
;      [else ....sym ... (first word)
;                        (interperse sym (rest word))]
;    )
;  )

(define (interperse sym word)
  (cond
    [(empty? word) (list (list sym))]
    [else (cons (cons sym  word)
          (prepend (first word)     (interperse sym (rest word))))
          ]
    )
  )


;EXAMPLES
(interperse 'd (list 'e 'r) )
;gives
(list (list 'd 'e 'r)
       (list 'e 'd 'r)
       (list 'e 'r 'd))

(interperse 'd empty)
;gives
(list 'd)

(interperse 'd '(x))
;gives
(list (list 'd 'x)
       (list 'x 'd))

(interperse 'f (list 'd 'a 'r 'k))
;gives
(list (list 'f 'd 'a 'r 'k)
      (list 'd 'f 'a 'r 'k)
      (list 'd 'a 'f 'r 'k)
      (list 'd 'a 'r 'f 'k)
      (list 'd 'a 'r 'k 'f)
      )




----Original Message Follows----
From: Matthias Felleisen <matthias at ccs.neu.edu>
To: "wooks wooks" <wookiz at hotmail.com>
CC: plt-scheme at list.cs.brown.edu
Subject: Re: [plt-scheme] HTDP 12.4.2
Date: Fri, 23 Jun 2006 20:46:56 -0400

No deal. It's not what you need according to the design.

Let's try this: formulate a precise purpose statement for intersperse and 
apply it to

  (intersperse letter (rest word))

i.e., the recursive call. Perhaps you will then see what you have to do with 
the result to get what you want for

  (intersperse letter word)

i.e., the original call. Now keep in mind that you must use the same words 
to describe both. It just has to fall out for the case above.

-- Matthias




On Jun 23, 2006, at 7:36 PM, wooks wooks wrote:

>
>If the recursive call
>
>(interperse sym (rest word))
>
>were to take  values as follows
>
>sym          (rest word)
>
>df                ark
>daf                rk
>darf                k
>darkf              empty
>
>then I'd be smoking  - but that means that the contract for interperse has 
>to change to
>
>interperse : word  word : list-of-words
>
>So I'd use (first word) by inserting it in the penultimate position in sym 
>right before the recursive call.
>
>Deal or no deal?
>
>----Original Message Follows----
>From: Matthias Felleisen <matthias at ccs.neu.edu>
>To: "wooks wooks" <wookiz at hotmail.com>
>CC: plt-scheme at list.cs.brown.edu
>Subject: Re: [plt-scheme] HTDP 12.4.2
>Date: Fri, 23 Jun 2006 18:35:48 -0400
>
>Much easier. Put (first word) back on.
>
>On Jun 23, 2006, at 6:21 PM, wooks wooks wrote:
>
>>
>>
>>The only thing I can see is that (first word) is not being used but I 
>>can't see where to slot it.
>>My best idea yet is another auxiliary function to collate all substrings 
>>of word that start from the beginnig of word and to merge that with the 
>>output I am getting below.
>>
>>
>>
>>----Original Message Follows----
>>From: Matthias Felleisen <matthias at ccs.neu.edu>
>>To: "wooks wooks" <wookiz at hotmail.com>
>>CC: plt-scheme at list.cs.brown.edu
>>Subject: Re: [plt-scheme] HTDP 12.4.2
>>Date: Fri, 23 Jun 2006 15:15:08 -0400
>>
>>
>>On Jun 23, 2006, at 3:05 PM, wooks wooks wrote:
>>
>>>(interperse 'f (list 'd 'a 'r 'k))
>>>
>>>gives
>>>
>>>(list (list 'f 'd 'a 'r 'k) (list 'f 'a 'r 'k) (list 'f 'r 'k) (list 'f 
>>>'k) 'f)
>>>
>>>and I want
>>>
>>>(list (list 'f 'd 'a 'r 'k) (list 'd 'f 'a 'r 'k) (list 'd 'a 'f 'r 'k) 
>>>(list 'd 'a 'r 'f 'k) (list 'd 'a 'r 'k 'f))
>>
>>Change the formating of the two things and take a close look:
>>
>>(list (list 'f 'd 'a 'r 'k) (list 'f 'a 'r 'k)    (list 'f 'r 'k)       
>>(list 'f 'k)                            'f)
>>(list (list 'f 'd 'a 'r 'k) (list 'd 'f 'a 'r 'k) (list 'd 'a 'f 'r 'k) 
>>(list 'd 'a 'r 'f 'k) (list 'd 'a 'r 'k 'f))
>>
>>When you recur with (rest word) you are dropping the first letter, at each 
>>stage.  So not surprisingly, the prefix is lost.
>>
>>Try with an example of length two and see whether you can come up with a 
>>better idea.
>>
>>
>
>
>_________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme




Posted on the users mailing list.