exercise-35.scm (543B)
1 (define-library (sicp solutions chapter-2 exercise-35) 2 (import (scheme base)) 3 (import (sicp utils)) 4 (export count-leaves count-leaves-2.2.2) 5 6 (begin 7 (define (count-leaves-2.2.2 tree) 8 (cond 9 ((null? tree) 0) 10 ((not (pair? tree)) 1) 11 (else (+ (count-leaves (car tree)) 12 (count-leaves (cdr tree)))))) 13 14 (define (count-leaves t) 15 (accumulate 16 + 17 0 18 (map (lambda (x) 19 (if (pair? x) 20 (count-leaves x) 21 1)) 22 t)))))