乐正

Actions speak louder than words.

Sicp-ex2-35

问题

将2.2.2节的count-leaves重定义为一个积累:

1
2
(define (count-leaves t)
  (accumulate <??> <??> (map <??> <??>)))

解答

练习2.35 (ex2-35.scm) download
1
2
3
4
5
6
7
;; Happy hacking Yuez - Emacs ♥ you!

(define (count-leaves t)
  (accumulate (lambda (x y) (+ 1 y))
              0
              (map (lambda (x) x)
                   (enumerate-tree t))))

测试

1
2
3
4
5
(count-leaves (list 1 (list 2 (list 3 4)) 5))
;Value: 5

(count-leaves (list 1 2 3))
;Value: 3

draft

« sicp-ex2-34 sicp-ex2-36 »

Comments