SICP問題2.35
2.2.2節の count-leaves
(define (count-leaves x) (cond ((null? x) 0) ((not (pair? x)) 1) (else (+ (count-leaves (car x)) (count-leaves (cdr x))))))
を accumulation として再定義せよ。
こんなので良いか
(define (count-leaves t) (accumulate (lambda (x y) (+ 1 y)) 0 (enumerate-tree t)))
と思ったんだけど、map 使えと書いてあるので、こんな感じで。
(define (count-leaves t) (accumulate + 0 (map (lambda (x) (cond ((null? x) 0) ((not (pair? x)) 1) (else (count-leaves x)))) t)))
メモ。
accumulate op initial sequence => result
map proc sequence => list
となる。