SICP Exercise 2.35
Question
Redefine count-leaves
from 2.2.2 as an accumulation:
(define (count-leaves t)
(accumulate ⟨??⟩ ⟨??⟩ (map ⟨??⟩ ⟨??⟩)))
Answer
(define (count-leaves t)
(accumulate +
0
(map (λ (x)
(if (pair? x)
(count-leaves x)
1))
t)))
; testing:
(define x (cons (list 1 2) (list 3 4)))
(count-leaves (list x x))
Results:
8