about summary refs log tree commit diff stats
path: root/js/games/nluqo.github.io/~bh/61a-pages/Lectures/2.2/tree1.scm
blob: 9fbfc118e018c38231b67890f8742d1793f1b59c (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
;;; Trees, version 1
;;;
;;; Data at all nodes, no empty tree defined.

(define make-tree cons)
(define datum car)
(define children cdr)

(define (leaf? node)
  (null? (children node)) )

(define (treemap fn tree)
  (make-tree (fn (datum tree))
	     (map (lambda (t) (treemap fn t))
		  (children tree) )))

;; Sample

(define (square x) (* x x))

(define (leaves . seq)
  (map (lambda (x) (make-tree x '())) seq))

(define t1
  (make-tree 1
	     (list (make-tree 2 (leaves 3 4))
		   (make-tree 5 (leaves 6 7 8)) )))