-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2.35.scm
29 lines (24 loc) · 874 Bytes
/
2.35.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(define (enumerate-tree tree)
(cond ((null? tree) '())
((not (pair? tree)) (list tree))
(else (append (enumerate-tree (car tree))
(enumerate-tree (cdr tree))))))
(define (count-leaves t)
(accumulate +
0
(map (lambda (x) 1) (enumerate-tree t))))
(count-leaves '((((1 2) (3 4))) ((5 6))))
;; copy from solution of scheme-wiki
;; I don't understand this
(define (count-leaves-recursive t)
(accumulate + 0 (map (lambda (node)
(if (pair? node)
(count-leaves-recursive node)
1))
t)))
(count-leaves-recursive '((((1 2) (3 4))) ((5 6))))