blob: 42686ecc7adda27a5cee76a7fa5ab54ecd365a65 (
plain)
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
|
;;; 6.945 Problem Set #8 Source Code
;;; 04/19/2009
;;; Bryan Newbold <bnewbold@mit.edu>
(load "load")
;;;------------------------------------------------------------------------
;;; Problem 8.2B:
(define the-empty-stream (stream))
(define (lazy-fringe subtree)
(define (lazy-walk subtree ans)
(cond ((pair? subtree)
(lazy-walk (car subtree)
(lazy-walk (cdr subtree) ans)))
((null? subtree) ans)
(else (cons-stream subtree ans))))
(lazy-walk subtree the-empty-stream))
(define (lazy-same-fringe? tree1 tree2)
(let lp ((f1 (lazy-fringe tree1))
(f2 (lazy-fringe tree2)))
(cond ((and (stream-null? f1) (stream-null? f2)) #t)
((or (stream-null? f1) (stream-null? f2)) #f)
((eq? (stream-car f1) (stream-car f2))
(lp (stream-cdr f1) (stream-cdr f2)))
(else #f))))
#| Test it!
(lazy-same-fringe?
'((a b) c ((d)) e (f ((g h))))
'(a b c ((d) () e) (f (g (h)))))
;Value: #t
(lazy-same-fringe?
'((a b) c ((d)) e (f ((g h))))
'(a b c ((d) () e) (g (f (h)))))
;Value: #f
|#
;;;------------------------------------------------------------------------
;;; Problem 8.5:
(define (make-pipe)
(let ((queue (queue:make))
(lock (conspire:make-lock)))
(list lock queue)))
(define (pipe-writer p)
(lambda (thing)
(conspire:acquire-lock (car p))
(queue:add-to-end! (cadr p) thing)
(conspire:unlock (car p))))
(define (pipe-reader p)
(lambda ()
(let lp ((nothing-here 0))
(conspire:acquire-lock (car p))
(cond ((queue:empty? (cadr p))
(conspire:unlock (car p))
(conspire:thread-yield)
(lp 0))
(else
(let ((ans (queue:get-first (cadr p))))
(conspire:unlock (car p))
ans))))))
#| Test!
(with-conspiracy
(lambda ()
(define test-pipe (make-pipe))
(define reader (pipe-reader test-pipe))
(define writer (pipe-writer test-pipe))
(writer 10)
(writer 20)
(reader)))
;Value: 10
|#
(define *done* (list '*done*))
(define (piped-same-fringe? tree1 tree2)
(let ((p1 (make-pipe)) (p2 (make-pipe)))
(let ((thread1
(conspire:make-thread
conspire:runnable
(lambda ()
(piped-fringe-generator tree1 (pipe-writer p1)))))
(thread2
(conspire:make-thread
conspire:runnable
(lambda ()
(piped-fringe-generator tree2 (pipe-writer p2)))))
(f1 (pipe-reader p1))
(f2 (pipe-reader p2)))
(let lp ((x1 (f1)) (x2 (f2)))
(cond ((and (eq? x1 *done*) (eq? x2 *done*)) #t)
((or (eq? x1 *done*) (eq? x2 *done*)) #f)
((eq? x1 x2) (lp (f1) (f2)))
(else #f))))))
(define (piped-fringe-generator tree return)
(define (lp tree)
(cond ((pair? tree)
(lp (car tree))
(lp (cdr tree)))
((null? tree) unspecific)
(else
(return tree))))
(lp tree)
(return *done*))
#| Test some more!
(with-conspiracy
(lambda ()
(piped-same-fringe? '((a b) c ((d)) e (f ((g h))))
'(a b c ((d) () e) (f (g (h)))))))
;Value: #t
(with-conspiracy
(lambda ()
(piped-same-fringe? '((a b) c ((d)) e (f ((g h))))
'(a b c ((d) () e) (g (g (h)))))))
;Value: #f
|#
;;;------------------------------------------------------------------------
;;; Problem 8.6:
(define (make-threaded-filter thunker)
(let ((pipe (make-pipe)))
(conspire:make-thread
conspire:runnable
(thunker (pipe-writer pipe)))
(pipe-reader pipe)))
#| Testy!
(with-conspiracy
(lambda ()
(make-threaded-filter (lambda x x))))
;Value: #[compound-procedure 31]
(with-conspiracy
(lambda ()
(define make-an-int
(let ((count 0))
(lambda ()
(set! count (+ count 1))
count)))
(define (return-an-int return)
(return (make-an-int)))
(return-an-int pp)
(define go
(make-threaded-filter return-an-int))
(go)))
;Value: 2
|#
(define *done* (list '*done*))
(define (tf-piped-same-fringe? tree1 tree2)
(let ((f1 (make-threaded-filter (tf-piped-fringe-generator tree1)))
(f2 (make-threaded-filter (tf-piped-fringe-generator tree2))))
(let lp ((x1 (f1)) (x2 (f2)))
(cond ((and (eq? x1 *done*) (eq? x2 *done*)) #t)
((or (eq? x1 *done*) (eq? x2 *done*)) #f)
((eq? x1 x2) (lp (f1) (f2)))
(else #f)))))
(define (tf-piped-fringe-generator tree)
(lambda (return)
(define (lp tree)
(cond ((pair? tree)
(lp (car tree))
(lp (cdr tree)))
((null? tree) unspecific)
(else
(return tree))))
(lp tree)
(return *done*)))
#|
(with-time-sharing-conspiracy
(lambda ()
(tf-piped-same-fringe?
'((a b) c ((d)) e (f ((g h))))
'(a b c ((d) () e) (f (g (h)))))
))
;Value: #t
(with-time-sharing-conspiracy
(lambda ()
(tf-piped-same-fringe?
'((a b) c ((d)) e (f ((g h))))
'(a b c ((d) () e) (g (f (h)))))
))
;Value: #f
(with-time-sharing-conspiracy
(lambda ()
(tf-piped-same-fringe?
'((a b) c ((d)) e (f ((g h))))
'(a b c ((d) () e) (g (f ))))
))
;Value: #f
|#
|