-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.3.3_1.rkt
199 lines (160 loc) · 5.49 KB
/
2.3.3_1.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#lang sicp
(define (test-equal a b)
(if (equal? a b)
#t
(error "Not equal" a b)))
; (test-equal "44" "55")
"sets as unordered lists"
(define (element-of-set? x set)
(cond ((null? set) false)
((equal? x (car set)) true)
(else (element-of-set? x (cdr set)))))
(define (adjoin-set x set)
(if (element-of-set? x set)
set
(cons x set)))
(define (intersection-set set1 set2)
(cond ((or (null? set1) (null? set2))
'())
((element-of-set? (car set1) set2)
(cons (car set1)
(intersection-set (cdr set1)
set2)))
(else (intersection-set (cdr set1)
set2))))
"2.59"
"Implement union-set for an unordered list set representation"
(define (union-set set1 set2)
(if (null? set1)
set2
(if (element-of-set? (car set1) set2)
(union-set (cdr set1) set2)
(cons (car set1) (union-set (cdr set1) set2)))))
(test-equal (union-set (list 1 2 3) (list 2 3 4)) (list 1 2 3 4))
"2.60"
"What if we allow duplicates?"
; I think I have done this problem (or a derivative of it) elsewhere
; element-of-set? doesn't change, it still goes through the whole list.
(test-equal (element-of-set? 1 (list 1)) #t)
; adjoin-set is just a call to cons, without needing element-of-set
(define (adjoin-set-d x set)
(cons x set))
(test-equal (adjoin-set-d 3 (list 2)) (list 3 2))
; union-set is just a call to append, without needing element-of-set
(define (union-set-d set1 set2)
(if (null? set1)
set2
(cons (car set1) (union-set-d (cdr set1) set2))))
(test-equal '(1 2 3 4) (union-set-d '(1 2) '(3 4)))
; intersection-set doesn't change, it still goes through every element of set1
(test-equal '(1 1 3) (intersection-set '(1 1 2 3) '(3 1)))
;; ToDo Calculate the time complexity.
;; Sets as ordered lists
"2.61"
"Adjoin-set for ordered representations."
(define (adjoin-set-o x set)
(cond ((= x (car set)) set) ; already a member
((< x (car set)) (cons x set)) ; x < first element
(else (cons (car set) (adjoin-set-o x (cdr set)))))) ; x > first element
(test-equal '(1 2 3 4) (adjoin-set-o 3 '(1 2 4)))
"2.62"
"Give an Θ(n) implementation of union-set for sets represented as ordered lists."
(define (union-set-o set1 set2)
(cond ((null? set1)
set2)
((null? set2)
set1)
((= (car set1) (car set2))
(cons (car set1) (union-set-o (cdr set1) (cdr set2))))
((< (car set1) (car set2))
(cons (car set1) (union-set-o (cdr set1) set2)))
(else ; (> (car set1) (car set2))
(cons (car set2) (union-set-o set1 (cdr set2))))))
(test-equal '(0 1 2 4 5) (union-set-o '(0 2 4 5) '(1 2 4)))
(test-equal '(1) (union-set-o '() '(1)))
(test-equal '(1) (union-set-o '(1) '()))
; Sets using trees.
(define (entry tree) (car tree))
(define (left-branch tree) (cadr tree))
(define (right-branch tree) (caddr tree))
(define (make-tree entry left right)
(list entry left right))
(define (element-of-set-t? x set)
(cond ((null? set) false)
((= x (entry set)) true)
((< x (entry set))
(element-of-set-t?
x
(left-branch set)))
((> x (entry set))
(element-of-set-t?
x
(right-branch set)))))
"2.63"
"Comparing tree-to-list procedures."
(define tree-2-16-1 '(7 (3 (1 () ()) (5 () ())) (9 () (11 () ()))))
(define tree-2-16-2 '(3 (1 () ()) (7 (5 () ()) (9 () (11 () ())))))
(define tree-2-16-3 '(5 (3 (1 () ()) ()) (9 (7 () ()) (11 () ()))))
; 1. Do the two procedures produce the same result for every tree?
; If not, how do the results differ?
; What lists do the two procedures produce for the trees in Figure 2.16?
(define (tree->list-1 tree)
(if (null? tree)
'()
(append
(tree->list-1
(left-branch tree))
(cons (entry tree)
(tree->list-1
(right-branch tree))))))
(define (tree->list-2 tree)
(define (copy-to-list tree result-list)
(if (null? tree)
result-list
(copy-to-list
(left-branch tree)
(cons (entry tree)
(copy-to-list
(right-branch tree)
result-list)))))
(copy-to-list tree '()))
(tree->list-1 tree-2-16-1)
(tree->list-1 tree-2-16-2)
(tree->list-1 tree-2-16-3)
(tree->list-2 tree-2-16-1)
(tree->list-2 tree-2-16-2)
(tree->list-2 tree-2-16-3)
"2.64"
"partial-tree"
(define (list->tree elements)
(car (partial-tree
elements (length elements))))
(define (partial-tree elts n)
(if (= n 0)
(cons '() elts)
(let ((left-size
(quotient (- n 1) 2)))
(let ((left-result
(partial-tree
elts left-size)))
(let ((left-tree
(car left-result))
(non-left-elts
(cdr left-result))
(right-size
(- n (+ left-size 1))))
(let ((this-entry
(car non-left-elts))
(right-result
(partial-tree
(cdr non-left-elts)
right-size)))
(let ((right-tree
(car right-result))
(remaining-elts
(cdr right-result)))
(cons (make-tree this-entry
left-tree
right-tree)
remaining-elts))))))))
(list->tree '(1 3 5 7 9 11))