forked from felipap/sicp-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAB.scm
169 lines (112 loc) · 2.73 KB
/
AB.scm
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
; Lecture: 10B
; Lecturer: Gerald Jay Sussman
;# SLIDE 0:10:25
(assign a (car (fetch b)))
====>
(assign a (vector-ref (fetch the-cars) (fetch b)))
(assign a (cdr (fetch b)))
====>
(assign a (vector-ref (fetch the-cdrs) (fetch b)))
;# END SLIDE
;# SLIDE 0:11:10
(perfrom (set-car! (fetch a) (fetch b)))
====>
(perform (vector-set! (fetch the-cars) (fetch a) (fetch b)))
(perform (set-cdr! (fetch a) (fetch b)))
====>
(perform (vector-set! (fetch the-cdrs) (fetch a) (fetch b)))
;# END SLIDE
;# SLIDE 0:13:00
With freelist method of allocation
(assign a (cons (fetch b) (fetch c)))
====>
(assign a (fetch free))
(assign free (vector-ref (fetch the-cdrs) (fetch free)))
(perform (vector-set! (fetch the-cars) (fetch a) (fetch b)))
(perform (vector-set! (fetch the-cdrs) (fetch a) (fetch c)))
;# END SLIDE
;;;
;;; BREAK 0:17:25
;;;
;# SLIDE 0:21:15
A source of garbage
(define (rev-loop x y)
(if (null? x)
y
(rev-loop (cdr x) (cons (car x) y))))
(define (append u v)
(rev-loop (rev-loop u '()) v))
;# END SLIDE
;# SLIDE 0:28:45
gc (assign thing (fetch root))
(assign continue sweep)
mark (branch (not-pair? (fetch thing)) done)
pair (assign mark-flag (vector-ref (fetch the-marks) (fetch thing)))
(branch (= (fetch mark-flag) 1) done)
(perform (vector-set! (fetch the-marks) (fetch-thing) 1))
;# END SLIDE
;# SLIDE 0:29:05
mcar (push thing)
(push continue)
(assign continue mcdr)
(assign thing (vector-ref (fetch the-cars) (fetch thing)))
(goto mark)
;# END SLIDE
;# SLIDE 0:29:10
mcdr (pop continue)
(pop thing)
(assign thing (vector-ref (fetch the-cdrs) (fetch thing)))
(goto mark)
done (goto (fetch continue))
;# END SLIDE
;# SLIDE 0:32:05
sweep (assign free '())
(assign scan (-1+ (fetch memtop)))
slp (branch (negative? (fetch scan)) end)
(assign mark-flag (vector-ref (fetch the-marks) (fetch scan)))
(branch (= (fetch mark-flag) 1) unmk)
;# SLIDE 0:32:10
; this is called if current cell is unmarked
(perform (vector-set! (fetch the-cdrs) (fetch scan) (fetch free)))
(assign free (fetch scan))
(assign scan (-1+ (fetch scan)))
(goto slp)
;# END SLIDE
;# END SLIDE
;# SLIDE 0:32:15
unmk (perform (vector-set! (fetch the-marks) (fetch scan) 0))
(assign scan (-1+ (fetch scan)))
(goto slp)
end
;# END SLIDE
; about "Drum" memory http://en.wikipedia.org/wiki/Drum_memory
;;;
;;; BREAK 0:40:20
;;;
;# BOARD 0:45:30
(define diag1
(λ (p)
(if (safe? p p)
(inf)
3)))
(define inf
(λ ()
((λ(x)(x x))
(λ(x)(x x)))))
(diag1 diag1)
;# END BOARD
; proving the answer to the halting problem
;# BOARD 0:37:40
(define diag2
(λ (p)
(if (safe? p p)
(other-then (p p))
false)))
(define other-then
(λ (x)
(if (eq? x 'A)
'B
'A))
(diag2 diag2)
;# END BOARD
; THE END