-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise 3.24 make-table with same-key?.rkt
288 lines (259 loc) · 5.28 KB
/
Exercise 3.24 make-table with same-key?.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#lang racket
; Exercise 3.24. In the table implementations above, the keys are tested for equality using equal?
; (called by assoc). This is not always the appropriate test. For instance, we might have a table with
; numeric keys in which we don't need an exact match to the number we're looking up, but only a number
; within some tolerance of it. Design a table constructor make-table that takes as an argument a same-key?
; procedure that will be used to test "equality" of keys. make-table should return a dispatch procedure
; that can be used to access appropriate lookup and insert! procedures for a local table.
; S O L U T I O N
(require rnrs/mutable-pairs-6)
(require compatibility/mlist)
(define (make-table same-key?)
(let ((local-table (mlist '*table*)))
(define (lookup key-1 key-2)
(let ((subtable (assoc same-key? key-1 (mcdr local-table))))
(if subtable
(let ((record (assoc same-key? key-2 (mcdr subtable))))
(if record
(mcdr record)
false
)
)
false
)
)
)
(define (insert! key-1 key-2 value)
(let ((subtable (assoc same-key? key-1 (mcdr local-table))))
(if subtable
(let ((record (assoc same-key? key-2 (mcdr subtable))))
(if record
(set-cdr! record value)
(set-cdr!
subtable
(mcons (mcons key-2 value) (mcdr subtable))
)
)
)
(set-cdr!
local-table
(mcons (mlist key-1 (mcons key-2 value)) (mcdr local-table))
)
)
)
'ok
)
(define (print-table)
(define (print-table-internal table)
(cond
((null? table) (void))
((not (mpair? table)) (display "Table element not a pair"))
(else
(begin
(print-row (mcar table))
(print-table-internal (mcdr table))
)
)
)
)
(define (print-row row)
(define (print-row-internal r)
(cond
((null? r) (void))
((not (mpair? r)) (display "Table element not a pair"))
(else
(print-field (mcar r))
(print-row-internal (mcdr r))
)
)
)
(display (mcar row))
(display ":")
(newline)
(print-row-internal (mcdr row))
)
(define (print-field f)
(cond
((null? f) (void))
((not (mpair? f)) (display "Table element not a pair"))
(else
(display " ")
(display (mcar f))
(display ": ")
(display (mcdr f))
(newline)
)
)
)
(display "TABLE")
(newline)
(display "-----")
(newline)
(print-table-internal (mcdr local-table))
)
(define (dispatch m)
(cond
((eq? m 'lookup-proc) lookup)
((eq? m 'insert-proc!) insert!)
((eq? m 'print) print-table)
(else (error "Unknown operation -- TABLE" m))
)
)
dispatch
)
)
(define (assoc same-key-proc key records)
(cond
((null? records) false)
((same-key-proc key (mcar (mcar records))) (mcar records))
(else (assoc same-key-proc key (mcdr records)))
)
)
; Test Driver
(define (run-test return-type proc . args)
(define (print-item-list items first-time?)
(cond
((not (pair? items)) (void))
(else
(if (not first-time?)
(display ", ")
(void)
)
(print (car items))
(print-item-list (cdr items) false)
)
)
)
(display "Applying ")
(display proc)
(display " on: ")
(print-item-list args true)
(newline)
(let ((result (apply proc args)))
(if (not (eq? return-type 'none))
(display "Result: ")
(void)
)
(cond
((procedure? result) ((result 'print)))
; ((eq? return-type 'deque) (print-deque result))
((eq? return-type 'none) (void))
(else
(print result)
(newline)
)
)
)
(newline)
)
(define (execution-time proc . args)
(define start-time (current-milliseconds))
; (display start-time)
; (display " ")
(apply proc args)
(define end-time (current-milliseconds))
; (display end-time)
(display "Execution time of ")
(display proc)
(display ": ")
(- end-time start-time)
)
; Tests
(define (same? a b)
(equal? a b)
)
(define T1 (make-table same?))
((T1 'insert-proc!) 'math '+ 43)
((T1 'print))
((T1 'insert-proc!) 'letters 'b 98)
((T1 'print))
((T1 'insert-proc!) 'math '* 42)
((T1 'print))
((T1 'insert-proc!) 'letters 'a 96)
((T1 'print))
((T1 'insert-proc!) 'math '- 45)
((T1 'print))
(define T2 (make-table same?))
((T2 'insert-proc!) 'punctuation "," 32)
((T2 'print))
((T2 'insert-proc!) 'punctuation "!" 33)
((T2 'print))
((T2 'insert-proc!) 'punctuation ";" 42)
(display "T2 is: ")
(newline)
((T2 'print))
(display "T1 is: ")
(newline)
((T1 'print))
; Test Results
Welcome to DrRacket, version 6.11 [3m].
Language: racket, with debugging; memory limit: 512 MB.
'ok
TABLE
-----
math:
+: 43
'ok
TABLE
-----
letters:
b: 98
math:
+: 43
'ok
TABLE
-----
letters:
b: 98
math:
*: 42
+: 43
'ok
TABLE
-----
letters:
a: 96
b: 98
math:
*: 42
+: 43
'ok
TABLE
-----
letters:
a: 96
b: 98
math:
-: 45
*: 42
+: 43
'ok
TABLE
-----
punctuation:
,: 32
'ok
TABLE
-----
punctuation:
!: 33
,: 32
'ok
T2 is:
TABLE
-----
punctuation:
;: 42
!: 33
,: 32
T1 is:
TABLE
-----
letters:
a: 96
b: 98
math:
-: 45
*: 42
+: 43
>