-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrees.ss
More file actions
executable file
·167 lines (136 loc) · 3.33 KB
/
trees.ss
File metadata and controls
executable file
·167 lines (136 loc) · 3.33 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(define l1 (list 1 3 (list 5 7) 9))
(define l2 (list (list 7)))
(define l3 (cons 1 (cons 2 (cons 3 ( cons 4 ( cons 5 (cons 6 7)))))))
(define m
(cdr (cdr (cdr (cdr (cdr (cdr l3))))))
)
(define x1 (list 1 2 3))
(define y1 (list 4 5 6))
(define x2 (list (list 1 2) (list 3 4)))
(define (deep-reverse l)
(define (rev l result)
(cond ((null? l) result)
((not (pair? l)) l)
(else (rev (cdr l) (cons (rev (car l) (list)) result ) ))
)
)
(rev l (list))
)
(define x3 (list 1 2 3 4))
(define x4 (list (list 1 2 3 4) (list 5 6 7 8)))
(define x5 (list x4 x4))
(
define (fringe t)
(cond ((null? t) (list))
((not (pair? t)) (list t))
(else (append (fringe (car t)) (fringe (cdr t))))
)
)
(define (fringe1 t)
(define (f1-iter ls result)
(cond ((null? ls) result)
((not (pair? ls)) (cons ls result))
(else (f1-iter (car ls) (f1-iter (cdr ls) result)))
)
)
(f1-iter t (list))
)
(define (make-mobile left right)
(list left right)
)
(define (make-branch length1 structure)
(list length1 structure)
)
(define (left-branch mobile)
(car mobile)
)
(define (right-branch mobile)
(car (cdr mobile))
)
(define (branch-length branch)
(car branch)
)
(define (branch-structure branch)
(car (cdr branch))
)
(define mob (make-mobile (make-branch 1 (make-mobile (make-branch 10 11) (make-branch 60 70)) ) (make-branch 5 6) ))
(define mob1 (make-mobile (make-branch 1 (make-mobile (make-branch 1 1) (make-branch 1 1))) (make-branch 1 (make-mobile (make-branch 1 1) (make-branch 1 1)))))
(define (total-weight mobile)
(let ( (left (left-branch mobile)) (right (right-branch mobile)))
(+ (branch-weight left) (branch-weight right))
)
)
(define (branch-weight branch)
(let ((struct (branch-structure branch)))
(if (pair? struct)
(total-weight struct)
struct
)
)
)
(define (balanced? mobile)
(let ((left (left-branch mobile)) (right (right-branch mobile)))
(and
(if (pair? (branch-structure left))
(balanced? (branch-structure left))
#t
)
(if (pair? (branch-structure right))
(balanced? (branch-structure right))
#t
)
( =
(* (branch-length left) (branch-weight left))
(* (branch-length right) (branch-weight right))
)
)
)
)
(define tr1 (list (list 2 3) (list 3 4)))
(define (sq-tree tree)
(cond ((null? tree) (list))
((not (pair? tree)) (* tree tree))
(else (cons (square-tree (car tree)) (square-tree (cdr tree))) )
)
)
(define (sq-tree1 tree)
(map
(lambda (sub-tree)
(if (pair? sub-tree)
(sq-tree1 sub-tree)
(* sub-tree sub-tree)
)
)
tree
)
)
(define (tree-map fct tree)
(map
(lambda (sub-tree)
(if (pair? sub-tree)
(tree-map fct sub-tree)
(fct sub-tree)
)
)
tree
)
)
(define (tree-map1 fct tree)
(cond ((null? tree) (list))
((not (pair? tree)) (fct tree))
(else (cons (tree-map1 fct (car tree)) (tree-map1 fct (cdr tree))) )
)
)
(define (sq-tree2 tree) (tree-map square tree))
(define set (list 1 2 3))
(define (subsets s)
(if (null? s)
(list (list))
(let ( (rest (subsets (cdr s))) )
(append rest (map
(lambda (x) (append x (list (car s))))
rest)
)
)
)
)