[plt-scheme] HTDP 12.4.2

From: wooks wooks (wookiz at hotmail.com)
Date: Wed Jun 21 20:03:40 EDT 2006

This is an excercise that I skipped as being too difficult the first time I 
attempted HTDP.


I was on the verge of giving up again when I managed to write an  
insertEverywhere that (it appears to me at least) works to required 
specification. However the results I get from plugging it into the 
arrangements function provided by the excercise do not look right but I 
can't decipher what it is producing.

Here is my code

;insertEverywhere : symbol ('a - 'z) list (word) - list of lists
;returns a list of words constructed by interpersing symbol in  position of 
the word
(define (insertEverywhere sym letters)
  (infiltrate  sym empty  letters)

;infiltrate : symbol list list - list of words
; (define (infiltrate infiltrator front back) ...
; auxiliary function for insertEverywhere

;(define (infiltrate infiltrator front back)
;   (cond
;      [(empty? back) ....]
;      [(empty? front) ..(infiltrate infiltrator (list (first back)) (rest 
;      [else (..... (infiltrate infiltrator (append front (list (first 
back))) (rest back))]
;    )
;  )

(define (infiltrate infiltrator front back)
    [(empty? back) (append front (list infiltrator))]
    [(empty? front)
     (cons (cons infiltrator back)
           (infiltrate infiltrator (append (list (first back) )) (rest 
back))) ]
     (cons (append front (list infiltrator) back)
           (infiltrate infiltrator (append front (list (first back))) (rest 
(insertEverywhere 'g (cons 'a (cons 'b (cons 'c empty))))

;; arrangements : word  ->  list-of-words
;; to create a list of all rearrangements of the letters in a-word
(define (arrangements a-word)
    [(empty? a-word) (cons empty empty)]
    [else (insertEverywhere (first a-word)
            (arrangements (rest a-word)))]))

(arrangements  (cons 'a (cons 'b (cons 'c empty))))

Posted on the users mailing list.