[plt-scheme] HTDP 12.4.2
Strong progress but not quite a cigar.
Reformulated program appended. Pls study -- Matthia
;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))))]))
;CONTRACT
;prepend : symbol list-of-words -> list-of-words
;Generates a new list by prefixing each word in the list with symbol
#|
(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
(equal?
(prepend 'g (list
(list 'd 'a 'r 'k)
(list)
(list 'x 'g)
(list 'p)
(list 'j 'k 'l)
)
)
'this-is-not-a-test-so-what-do-you-expect?)
;EXAMPLES
(equal?
(interperse 'd (list 'e 'r) )
;gives
(list (list 'd 'e 'r)
(list 'e 'd 'r)
(list 'e 'r 'd))
)
(equal?
(interperse 'd empty)
;gives
(list 'd)
)
(equal?
(interperse 'd '(x))
;gives
(list (list 'd 'x)
(list 'x 'd))
)
(equal?
(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)
)
)