14 lines
413 B
Scheme
14 lines
413 B
Scheme
#lang sicp
|
|
|
|
(define (accumulate op initial sequence)
|
|
(if (null? sequence)
|
|
initial
|
|
(op (car sequence)
|
|
(accumulate op initial (cdr sequence)))))
|
|
|
|
(define (count-leaves t)
|
|
(accumulate + 0 (map (lambda (sub-t)
|
|
(cond ((pair? sub-t) (count-leaves sub-t))
|
|
((null? sub-t) 0)
|
|
(else 1)))
|
|
t)))
|