这个问题并不难,但是要迎合出题人的格式:
(define (count_leaves t)
(accumulate <??> <??> (map <??> <??>)))
Scheme代码:
> (define (count_leaves t)
(accumulate + 0 (map (lambda (t)
(if (pair? t)
(count_leaves t)
1)) t)))
运行结果:
> (count_leaves (list 1 (list 2 3 ) 4 5))
5
还是递归思想,要是遇到叶子节点计数 1,如果不是叶子节点递归调用自身count_leaves继续递归处理。