Dan's Thoughts Thinking somewhat carefully

18Aug/090

Exercise 2.33 of SICP

Exercise 2.33: Fill in the missing expressions to complete the following definitions of some basic list-manipulation operations as accumulations:

(define (map p sequence)
  (accumulate (lambda (x y) <??>) nil sequence))
(define (append seq1 seq2)
  (accumulate cons <??> <??>))
(define (length sequence)
  (accumulate <??> 0 sequence))
(define (accumulate fn init-value items)
  (if (null? items)
    init-value
    (fn (car items)
        (accumulate fn init-value (cdr items)))))
 
(define (map p sequence)
  (accumulate (lambda (x y) (cons (p x) y)) '() sequence))
 
(define (append seq1 seq2)
  (accumulate cons seq2 seq1))
 
(define (length seq)
  (accumulate (lambda (x y) (+ 1 y)) 0 seq))

I find it interesting how length is created by discarding the x in lambda thus discarding (car items).

Filed under: lisp, SICP Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

Spam protection by WP Captcha-Free

No trackbacks yet.