自学内容网 自学内容网

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

原文地址:https://blog.csdn.net/ilongchaos/article/details/142732171

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!