sicp每日一题[2.35]

十一去喀纳斯玩了2天,今天恢复,才几天没看书再看到代码就感到有点陌生了。。

Exercise 2.35

Redefine count-leaves from Section 2.2.2 as an accumulation:

(define (count-leaves t)
  (accumulate ⟨??⟩ ⟨??⟩ (map ⟨??⟩ ⟨??⟩)))

这道题难度不大,利用 enumerate-tree 和 accumulate 很容易就能实现。

(define (count-leaves t)
  (accumulate
   +
   0
   (map (lambda (x) (if (null? x) 0 1)) 
        (enumerate-tree t))))


(define x (cons (list 1 2) (list 1 3 0 5 0 1)))
(count-leaves x)
(count-leaves (list x x))

; 执行结果
10
20
posted @ 2024-10-06 21:30  再思即可  阅读(2)  评论(0编辑  收藏  举报