您的位置:首页 > 其它

SICP 习题2.33 用accumulate完成一些基本的表操作

2016-06-24 16:05 489 查看

这个accumulate比第一章完成更强大

(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))

(define (map p sequence)
(accumulate (lambda (x y) (cons (p x) y)) () sequence))

(define square
(lambda (x) (* x x)))

(newline)
(display (map square (list 1 2 3 4 5)))

(define (append seq1 seq2)
(accumulate cons seq2 seq1))

(newline)
(display (append (list 1 2 3) (list 4 5 6)))

(define (length sequence)
(accumulate (lambda (x y) (+ 1 y)) 0 sequence))

(newline)
(display (length (list 1 2 (list 3 4))))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: