-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.2.2-hierarchical-structures.rkt
178 lines (134 loc) · 3.92 KB
/
2.2.2-hierarchical-structures.rkt
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
167
168
169
170
171
172
173
174
175
176
177
178
#lang sicp
; exercise 2.24
; (list 1 (list 2 (list 3 4)))
; / \
; 1 (list 2 (list 3 4))
; / \
; 2 (list 3 4)
; / \
; 3 4
; exercise 2.25
(car (cdr (car (cdr (cdr (list 1 3 (list 5 7) 9))))))
(car (cdaddr (list 1 3 (list 5 7) 9)))
(car (car (list (list 7))))
(caar (list (list 7)))
(car (cdadar (cdadar (cdadr (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7))))))))))
; exercise 2.26
(define x (list 1 2 3))
(define y (list 4 5 6))
(append x y) ; '(1 2 3 4 5 6)
(cons x y) ; '((1 2 3) 4 5 6)
(list x y) ; '((1 2 3) (4 5 6))
; exercise 2.27
; from 2.18
(define (reverse xs)
(define (inner xs acc)
(if (null? xs)
acc
(inner (cdr xs) (cons (car xs) acc))))
(inner xs '()))
(define x (list (list 1 2) (list 3 4)))
(reverse x) ; '((3 4) (1 2))
(define (deep-reverse xs)
(if (not (pair? xs))
xs
(map deep-reverse (reverse xs))))
(deep-reverse x) ; '((4 3) (2 1))
(deep-reverse (list 1 (list 2 3 (list 4 5) 6) (list -1 -2))) ; '((-2 -1) (6 (5 4) 3 2) 1)
; exercise 2.28
(define (fringe xs)
(cond ((and (pair? xs) (not (null? (cdr xs))))
(append (fringe (car xs)) (fringe (cdr xs))))
((pair? xs) (fringe (car xs)))
(else (list xs))))
(fringe x) ; '(1 2 3 4)
(fringe (list 1 (list 2 3 (list 4 5) 6) (list -1 -2))) ; '(1 2 3 4 5 6 -1 -2)
; exercise 2.29
(define (assert expr)
(if (not expr)
(error "failed assertion")
true))
(define (make-mobile left right)
(list left right))
(define (make-branch length structure)
(list length structure))
(define left-branch car)
(define right-branch cadr)
(define branch-length car)
(define branch-structure cadr)
(define (mobile? branch)
(pair? (branch-structure branch)))
(define b0 (make-branch 10 5))
(define b1
(make-branch 7
(make-mobile (make-branch 3 3)
(make-branch 7 5))))
(assert (mobile? b1)) ; #t
(assert (not (mobile? b0))) ; #t
(define mobile
(make-mobile b0 b1))
(left-branch mobile) ; '(10 5)
(right-branch mobile) ; '(7 ((3 3) (7 2)))
(define (branch-weight branch)
(if (mobile? branch)
(weight (branch-structure branch))
(branch-structure branch)))
(branch-weight b0) ; 5
(branch-weight b1) ; 8
(define (weight mobile)
(+ (branch-weight (left-branch mobile))
(branch-weight (right-branch mobile))))
(weight mobile) ; 13
(define (torque branch)
(* (branch-length branch)
(branch-weight branch)))
(torque b0) ; 50
(torque b1) ; 56
(define (balanced? mobile)
(= (torque (left-branch mobile))
(torque (right-branch mobile))))
(balanced? mobile) ; #f
(define b2 (make-branch 4 14))
(balanced? (make-mobile b1 b2)) ; #t
; If we change the definition of the constructors to use `cons` instead of `list`,
; all we need to change is the definitions of `right-branch` and `branch-structure`
; from `cadr` to `cdr`. The reason being that for a list, cdr returns a singleton list
; whereas for a pair, it returns the value itself already "unwrapped".
; exercise 2.30
(define (square-tree-direct tree)
(cond ((null? tree) '())
((not (pair? tree)) (* tree tree))
(else
(cons (square-tree-direct (car tree))
(square-tree-direct (cdr tree))))))
(define tree (list 1
(list 2 (list 3 4) 5)
(list 6 7)))
(square-tree-direct tree) ; '(1 (4 (9 16) 25) (36 49))
(define (square-with-map tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(square-with-map sub-tree)
(* sub-tree sub-tree)))
tree))
(square-with-map tree) ; '(1 (4 (9 16) 25) (36 49))
; exercise 2.31
(define (tree-map f tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(tree-map f sub-tree)
(f sub-tree)))
tree))
(define (square-tree tree)
(tree-map (lambda (x) (* x x)) tree))
(square-tree tree) ; '(1 (4 (9 16) 25) (36 49))
; exercise 2.32
(define (subsets s)
(if (null? s)
(list '())
(let ((rest (subsets (cdr s))))
(append rest
(map (lambda (x)
(cons (car s) x))
rest)))))
(subsets (list 1 2 3)) ; '(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))