blob: 2c919329573b3a804c8fd5fbb8f8c28196eb4053 (
plain) (
blame)
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
30
31
32
33
34
35
36
37
38
39
40
41
|
;;; Trees, version 2
;;;
;;; Data at leaves only, leaves are words
(define (make-tree d ch)
(cond ((null? d) ch)
((null? ch) d)
(else (error "Data at leaves only")) ))
(define (datum node)
(cond ((null? node) '())
((not (pair? node)) node)
(else '()) ))
(define (children node)
(cond ((null? node) '())
((not (pair? node)) '())
(else node) ))
(define (leaf? node)
(not (pair? node)))
(define (treemap fn tree)
(cond ((null? tree) '())
((leaf? tree) (fn tree))
(else (map (lambda (t) (treemap fn t)) tree)) ))
;; Sample
(define (square x) (* x x))
(define t2 '((1 2) 3 (4 (5 6))))
(define t3 (make-tree '()
(list (make-tree '() (list (make-tree 1 '())
(make-tree 2 '())))
(make-tree 3 '())
(make-tree '() (list (make-tree 4 '())
(make-tree '() (list (make-tree 5 '())
(make-tree 6 '()))))))))
|