-
Notifications
You must be signed in to change notification settings - Fork 0
/
excercise3.12~3.20.rkt
425 lines (370 loc) · 13.5 KB
/
excercise3.12~3.20.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#lang sicp
;; Excercise 3.12:
;; The following procedure for appending lists was introduced
;; in Section 2.2.1:
(define (append x y)
(if (null? x)
y
(cons (car x) (append (cdr x) y))))
;; Append forms a new list by successively consing the elements of
;; x onto y. The procedure append! is similar to append, but it is a
;; mutator rather than a constructor. It appends the lists by splicing
;; them together, modifying the final pair of x so that its cdr is now y.
;; (It is an error to call append! with an empty x.)
(define (append! x y)
(set-cdr! (last-pair x) y)
x)
;; Here last-pair is a procedure that returns the last pair
;; in its argument:
(define (last-pair x)
(if (null? (cdr x))
x
(last-pair (cdr x))))
;; Consider the interaction
(define x (list 'a 'b))
(define y (list 'c 'd))
(define z (append x y))
; z
; => (a b c d)
; (cdr x)
; <response>
(define w (append! x y))
; w
; => (a b c d)
; (cdr x)
; <response>
;; What are the missing <response>s? Draw box-and-poiter diagrams to explain your answer.
;; the first (cdr x) => (b)
;; the second (cdr x) => (b c d)
;; Because z is represent as:
;;x --> [*]----> [*]----> '()
;; | |
;; v v
;; 'a 'b
;;
;;y --> [*]----> [*]----> '()
;; | |
;; v v
;; 'c 'd
;;
;;z --> [*]---->[*]---->[*]---->[*]----> '()
;; | | | |
;; v v v v
;; 'a 'b 'c 'd
;; and for the second one:
;;w------+
;; |
;; |
;; v
;;x --> [*]----> [*]----+
;; | | |
;; v v |
;; 'a 'b |
;; |
;; +--------------+
;; |
;; v
;;y --> [*]----> [*]----> '()
;; | |
;; v v
;; 'c 'd
;;
;;z --> [*]---->[*]---->[*]---->[*]----> '()
;; | | | |
;; v v v v
;; 'a 'b 'c 'd
;; Excercise 3.13:
;; Consider the following make-cycle procedure,
;; which uses the last-pair procedure defined in Exercise 3.12:
(define (make-cycle x)
(set-cdr! (last-pair x) x) x)
;; Draw a box-and-pointer diagram that shows the structure zs created
;; by:
(define zs (make-cycle (list 'a 'b 'c)))
;; What happens if we try to compute (last-pair zs)?
;; it will compute an infinite loop, because:
;; +-----------------------+
;; | |
;; v |
;;zs ----> [*]----> [*]----> [*]----+
;; | | |
;; v v v
;; 'a 'b 'c
;; Excercise 3.14:
;; The following procedure is quite useful, although
;; obscure:
(define (mystery x)
(define (loop x y)
(if (null? x)
y
(let ((temp (cdr x)))
(set-cdr! x y)
(loop temp x))))
(loop x '()))
;; Loop uses the “temporary” variable temp to hold the old value of
;; the cdr of x, since the set-cdr! on the next line destroys the cdr.
;; Explain what mystery does in general. Suppose v is defined by
;; (define v (list ’a ’b ’c ’d)). Draw the box-and-pointer diagram
;; that represents the list to which v is bound. Suppose that we
;; now evaluate (define w (mystery v)). Draw box-and-pointer
;; diagrams that show the structures v and w after evaluating this expression.
;; What would be printed as the values of v and w?
;; 实际上mystery是一个修改版的reverse函数:
;;v --> [*]----> [*]----> [*]----> '()
;; | | |
;; v v v
;; 'a 'b 'c
;; (mystery v)
;; (mystery (list 'a 'b 'c))
;;
;; (loop (list 'a 'b 'c) '())
;;
;; (let ((temp (list 'b 'c)))
;; (set-cdr! (list 'a 'b 'c) '())
;; (loop (list 'b 'c) (list 'a)))
;;
;; (loop (list 'b 'c) (list 'a))
;;
;; (let ((temp (list 'c)))
;; (set-cdr! (list 'b 'c) (list 'a))
;; (loop (list 'c) (list 'b 'a)))
;;
;; (loop (list 'c) (list 'b 'a))
;;
;; (let ((temp '()))
;; (set-cdr! (list 'c) (list 'b 'a))
;; (loop '() (list 'c 'b 'a)))
;;
;; (loop '() (list 'c 'b 'a))
;;
;; (list 'c 'b 'a)
;;v------------------------+
;; |
;; v
;;w --> [*]----> [*]----> [*]----> '()
;; | | |
;; v v v
;; 'c 'b 'a
(define m (list 'a 'b))
(define z1 (cons m m))
(define z2 (cons (list 'a 'b) (list 'a 'b)))
(define (set-to-wow! x)
(set-car! (car x) 'wow) x)
;; Excercise 3.15:
;; Draw box-and-pointer diagrams to explain the effect
;; of set-to-wow! on the structures z1 and z2 above.
;;z1 --> [*][*]
;; | |
;; v v
;; x --> [*][*]--> [*][/]
;; | |
;; v v
;; 'wow! 'b
;;z2 --> [*][*]--> [*][*]--> [*][/]
;; | | |
;; | v v
;; | 'a 'b
;; | ^
;; | |
;; +------> [*][*]--> [*][/]
;; |
;; v
;; 'wow!
;; Excercise 3.16:
;; Ben Bitdiddle decides to write a procedure to count
;; the number of pairs in any list structure. “It’s easy,” he reasons.
;; “The number of pairs in any structure is the number in the car plus
;; the number in the cdr plus one more to count the current pair.” So
;; Ben writes the following procedure:
(define (count-pairs x)
(if (not (pair? x))
0
(+ (count-pairs (car x))
(count-pairs (cdr x))
1)))
;; Show that this procedure is not correct. In particular, draw boxand-
;; pointer diagrams representing list structures made up of exactly
;; three pairs for which Ben’s procedure would return 3; return 4
;; return 7; never return at all.
(define str1 '(foo bar baz))
(count-pairs str1) ; 3
; str1 -> ( . ) -> ( . ) -> ( . ) ->null
; | | |
; v v v
; 'foo 'bar 'baz
(define xx '(foo))
(define yy (cons xx xx))
(define str2 (list yy))
(count-pairs str2) ; 4
; str2 -> ( . ) -> null
; |
; v
; ( . )
; | |
; v v
; ( . ) -> 'null
; |
; v
; 'foo
(define str3 (cons yy yy))
(count-pairs str3) ; 7
; str3 -> ( . )
; | |
; v v
; ( . )
; | |
; v v
; ( . ) -> null
; |
; v
; 'foo
(define str4 '(foo bar baz))
(set-cdr! (cddr str4) str4)
;(count-pairs str4) ; maximum recursion depth exceeded
; ,-------------------,
; | |
; v |
; str4 -> ( . ) -> ( . ) -> ( . )
; | | |
; v v v
; 'foo 'bar 'baz
;; Excercise 3.17:
;; Devise a correct version of the count-pairs procedure
;; of Exercise 3.16 that returns the number of distinct pairs in
;; any structure. (Hint: Traverse the structure, maintaining an auxiliary
;; data structure that is used to keep track of which pairs have
;; already been counted.)
(define (count-real-pairs x)
(length (inner x '())))
(define (false? v)
(if (pair? v)
#f
#t))
(define (inner x memo-list)
(if (and (pair? x) (false? (memq x memo-list)))
(inner (car x)
(inner (cdr x)
(cons x memo-list)))
memo-list))
;; Excercise 3.18:
;; Write a procedure that examines a list and determines
;; whether it contains a cycle, that is, whether a program that
;; tried to find the end of the list by taking successive cdrs would go
;; into an infinite loop. Exercise 3.13 constructed such lists.
;; 网上抄的解法,因为第一次想就已经找到常量空间解法了。
(define (isloop? lst)
(let ((identity (cons '() '())))
(define (iter remain-list)
(cond ((null? remain-list)
#f)
((eq? identity (car remain-list))
#t)
(else
(set-car! remain-list identity)
(iter (cdr remain-list)))))
(iter lst)))
;; Excercise 3.19:
;; Redo Exercise 3.18 using an algorithm that takes
;; only a constant amount of space. (This requires a very clever idea.)
;; 使用快慢步解决这个问题,快的每次走两步,慢的每次走一步,如果有环那么肯定会相遇
(define (loop? lst)
(define (safe-cdr l)
(if (pair? l)
(cdr l)
'()))
(define (iter a b)
(cond [(or (null? a) (null? b)) #f]
[(eq? a b) #t]
[(eq? a (safe-cdr b)) #t]
[else
(iter (safe-cdr a) (safe-cdr (safe-cdr b)))]))
(iter (safe-cdr lst) (safe-cdr (safe-cdr lst))))
;; Excercise 3.20:
;; Draw environment diagrams to illustrate the evaluation
;; of the sequence of expressions
;(define x (cons 1 2))
;(define z (cons x x))
;(set-car! (cdr z) 17)
;(car x)
;17
;; using the procedural implementation of pairs given above
;; (Compare Excercise 3.11.)
;; +------------------------------+
;;global -> | |
;;env | x |
;; +--|---------------------------+
;; | ^
;; | |
;; | +----------+
;; | E1 -> | x: 1 |
;; | | y: 2 |
;; | | |
;; | | set-x! -----> ...
;; | | set-y! -----> ...
;; +--------->dispatch ---> parameters: m
;; | | body: (cond ((eq? m 'car) 'car)
;; +----------+ ((eq? m 'cdr) 'cdr)
;; ((eq? m 'set-car!) 'set-car!)
;; ((eq? m 'set-cdr!) 'set-cdr!)
;; (else
;; (error "..." m)))
;; +-------------------------------------------------------+
;;global -> | |
;;env | z x |
;; +--|---------------------------|------------------------+
;; | ^ | ^
;; | | | |
;; | | | +----------+
;; | | | | x: 1 |
;; | | | | y: 2 |
;; | | | | |
;; | | | | set-x! -----> ...
;; | | | | set-y! -----> ...
;; | | +------->dispatch ---> parameters: m
;; | | | ^ ^ | body: ...
;; | | +--|-|-----+
;; | +----------+ | |
;; | E2 -> | x: ------------------------+ |
;; | | y: --------------------------+
;; | | |
;; | | set-x! -----> ...
;; | | set-y! -----> ...
;; +--------->dispatch ---> parameters: m
;; | | body: (cond ((eq? m 'car) 'car)
;; +----------+ ((eq? m 'cdr) 'cdr)
;; ((eq? m 'set-car!) 'set-car!)
;; ((eq? m 'set-cdr!) 'set-cdr!)
;; (else
;; (error "..." m)))
;; (set-car! (cdr z) 17)有以下两个步骤:
;; 1. 执行(cdr z)返回x
;; 2. 执行(set-car! x 17),引发表达式((x 'set-car!) 17)执行,然后引发(set-x! 17)
;; 最终x的car被设置为17
;; +-------------------------------------------------------+
;;global -> | |
;;env | z x |
;; +--|---------------------------|------------------------+
;; | ^ | ^
;; | | | |
;; | | | +----------+
;; | | | | x: 17 |
;; | | | | y: 2 |
;; | | | | |
;; | | | | set-x! -----> ...
;; | | | | set-y! -----> ...
;; | | +------->dispatch ---> parameters: m
;; | | | ^ ^ | body: ...
;; | | +--|-|-----+
;; | +----------+ | |
;; | E2 -> | x: ------------------------+ |
;; | | y: --------------------------+
;; | | |
;; | | set-x! -----> ...
;; | | set-y! -----> ...
;; +--------->dispatch ---> parameters: m
;; | | body: (cond ((eq? m 'car) 'car)
;; +----------+ ((eq? m 'cdr) 'cdr)
;; ((eq? m 'set-car!) 'set-car!)
;; ((eq? m 'set-cdr!) 'set-cdr!)
;; (else
;; (error "..." m)))