blob: 472fa193530c4abca6412eaafb4dcedeea92eac2 (
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
|
(define (try-two-ways thunk1 thunk2)
(let ((value) (done? #f))
(let ((thread1
(conspire:make-thread
conspire:runnable
(lambda ()
(set! value (thunk1))
(set! done? #t))))
(thread2
(conspire:make-thread
conspire:runnable
(lambda ()
(set! value (thunk2))
(set! done? #t)))))
(conspire:switch-threads
(lambda () done?))
(conspire:kill-threads
(list thread1 thread2))
value)))
(define (test n1 n2)
(with-conspiracy
(lambda ()
(try-two-ways
(lambda ()
(let lp ((n n1))
(if (= n 0)
'a-done
(begin
(if (= (remainder n 100000) 0)
(begin (display 'a)
(conspire:thread-yield)))
(lp (- n 1))))))
(lambda ()
(let lp ((n n2))
(if (= n 0)
'b-done
(begin
(if (= (remainder n 100000) 0)
(begin (display 'b)
(conspire:thread-yield)))
(lp (- n 1))))))))))
#|
(test 1000000 1200000)
ababababababababababab
;Value: a-done
(test 1200000 1000000)
babababababababababaa
;Value: b-done
|#
(define (test1 n1 n2)
(with-time-sharing-conspiracy
(lambda ()
(try-two-ways
(lambda ()
(let lp ((n n1))
(if (= n 0)
'a-done
(begin
(if (= (remainder n 100000) 0)
(display 'a))
(lp (- n 1))))))
(lambda ()
(let lp ((n n2))
(if (= n 0)
'b-done
(begin
(if (= (remainder n 100000) 0)
(display 'b))
(lp (- n 1))))))))))
#|
(test1 1000000 1200000)
baabbaabbaabbaabbaabb
;Value: a-done
(test1 1200000 1000000)
babaabbaabbaabbaabbaa
;Value: b-done
|#
;;; Interesting example
;;; Suppose we want to search a list, that
;;; may be infinite (circular). We could
;;; use the fast algorithm, but sometimes
;;; go into an infinite loop, or we could
;;; use the slow algorithm that marks the
;;; list (with a hash table) but always
;;; works. If the statistics are right,
;;; a better strategy is to time-share the
;;; two methods and take the one which
;;; finishes first:
(define (safe-mem? item lst)
(let ((table (make-eq-hash-table)))
(let lp ((lst lst))
(if (pair? lst)
(if (hash-table/get table lst #f)
#f ;circular
(if (eq? item (car lst))
#t
(begin
(hash-table/put! table lst #t)
(lp (cdr lst)))))
#f))))
(define (unsafe-mem? item lst)
(let lp ((lst lst))
(if (pair? lst)
(if (eq? item (car lst))
#t
(lp (cdr lst)))
#f)))
#|
(define foo (list 'a 'b 'c 'd))
;Value: foo
(begin (set-cdr! (last-pair foo) foo) 'foo)
;Value: foo
(unsafe-mem? 'b foo)
;Value: #t
(unsafe-mem? 'e foo)
;Quit!
(safe-mem? 'b foo)
;Value: #t
(safe-mem? 'e foo)
;Value: #f
|#
(define (mem? item lst)
(with-time-sharing-conspiracy
(lambda ()
(try-two-ways
(lambda ()
(unsafe-mem? item lst))
(lambda ()
(safe-mem? item lst))))))
#|
(mem? 'b foo)
;Value: #t
(mem? 'e foo)
;Value: #f
|#
|