2.35 Exercise 2.35
I can think of a silly way to write count-leaves using the template provided
by the exercise:
(define (count-leaves t) | (accumulate (lambda (x y) (+ y 1)) | 0 | (map identity (enumerate-tree t)))) |
|
Of course, this could be simplified like this:
(define (count-leaves t) | (accumulate (lambda (x y) (+ y 1)) 0 (enumerate-tree t))) |
|
However, we don’t have to write count-leaves as an accumulation, because
we already have an accumulate procedure for counting things: length
from Exercise 2.33:
(define (count-leaves t) | (length (enumerate-tree t))) |
|