[plt-scheme] HTDP 21.1.2

From: wooks . (wookiz at hotmail.com)
Date: Mon Jul 10 12:05:43 EDT 2006

http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-27.html#node_sec_21.1

OK I'm flummoxed on the bit that asks you to use map to define fold.

;; to derive a summarisation of the input list by application of the 
specified function.
;; the first parameter is the value the fold returns for the base case.

(define (fold baseCase op alon)
  (cond
    [(empty? alon) baseCase]
    [else (op (first alon)
	     (fold baseCase op (rest alon)))]))

(define (sum alon)
  (fold 0 + alon))
(define (prod alon)
  (fold 1 * alon))

All good .... the append using fold was pretty straightforward but if I want 
to map square on to a list

(define (square n) (* n n))

(myMap square (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16))

I don't see how this can be done with fold.... is this a trick part of the 
question...(I suspect not).




Posted on the users mailing list.