#lang racket
(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 (x)
(if (pair? x) (count-leaves x) 1))
t)))
(count-leaves '(1 (3 (1 2 3) 0 (1 3 4 (2 3))) 5 0 1))
运行结果
14