-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch3-exercises.scm
More file actions
218 lines (186 loc) · 6.06 KB
/
ch3-exercises.scm
File metadata and controls
218 lines (186 loc) · 6.06 KB
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
;CHAPTER THREE : MODULARITY, OBJECTS, AND STATE
;SECTION 3.1: ASSIGNMENT AND LOCAL STATE
;3.1 A simple object with state
(define (make-accumulator total)
(lambda (amount)
(begin (set! total (+ amount total)))
total))
;3.2 Monitoring how many times a function is called
(define (make-monitored f)
(let ((count 0))
(define (how-many-calls?) count)
(define (reset-count) (set! count 0))
(define (dispatch m)
(cond ((eq? m 'how-many-calls?) (how-many-calls?))
((eq? m 'reset-count) (reset-count))
(else (begin (set! count (+ 1 count)) (f m)))))
dispatch))
;3.3 & 3.4 A more developed make-account
(define (make-account balance password)
(let ((error-count 0))
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient Funds"))
(define (deposit amount)
(begin (set! balance (+ balance amount))
balance))
(define (reset-error-count)
(set! error-count 0))
(define (test-error-count)
(begin (set! error-count (+ error-count 1))
(if (> error-count 7)
"Cops alerted"
"Incorrect Password")))
(define (dispatch pw m)
(if (not (eq? pw password)) (begin (test-error-count) false)
(begin (reset-error-count)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
((eq? m 'password?) true)
(else (error "Unknown request -- MAKE-ACCOUNT"
m))))))
dispatch))
;3.5 Some bas
(define (rand-0-to-1) (/ (random 100000000) 100000000.0))
(define (rand-interval a b) (+ a (* (- b a) (rand-0-to-1))))
(define (average-over-trials function trials)
(define (iter trials-remaining total)
(if (= trials-remaining 0)
(/ total trials)
(iter (- trials-remaining 1) (+ total (function)))))
(iter trials 0))
(define (monte-carlo trials experiment)
(define (iter trials-remaining trials-passed)
(cond ((= trials-remaining 0) (/ trials-passed trials))
((experiment) (iter (- trials-remaining 1) (+ trials-passed 1)))
(else (iter (- trials-remaining 1) trials-passed))))
(iter trials 0))
(define (estimate-integral-test x1 y1 x2 y2 predicate)
(lambda () (predicate (rand-interval x1 x2) (rand-interval y1 y2))))
(define (estimate-integral x1 y1 x2 y2 predicate trials)
(monte-carlo trials (estimate-integral-test x1 y1 x2 y2 predicate)))
(define (in-unit-circle? x y)
(<= (sqrt (+ (* x x) (* y y))) 1))
(define (estimate-pi trials)
(* 4.0 (estimate-integral -1.0 -1.0 1.0 1.0 in-unit-circle? trials)))
;3.7
(define (make-joint original orig-pw new-pw)
(define (test-pw test m)
(if (eq? new-pw test) (original orig-pw m)
"Wrong Password"))
(if (original orig-pw 'password?) test-pw
"Bad Password"))
;SECTION 3.2 THE ENVIRONMENT MODEL OF EVALUATION
;The many omitted problems here were mostly about drawing environment frames. I found most of them pretty trivial
;and not worth the effort to write down.
;3.18
(define (count-pairs x)
(let ((encountered nil))
(define (iter x)
(if (or (not (pair? x)) (memq x encountered))
0
(begin
(set! encountered (cons x encountered))
(+ (iter (car x))
(iter (cdr x))
1)
)
)
)
(iter x)
)
;3.19
(define (cycle? x)
(let ((encountered nil))
(define (iter x)
(set! encountered (cons x encountered))
(cond (or (not (pair? x)) (null? (cdr x)) false)
((memq (cdr x) visited) true)
(else (iter cdr x)))
)
)
(iter x)
)
;3.28
(define (or-gate a1 a2 output)
(define (or-action-procedure)
(let ((new-value (logical-or (get-signal a1) (get-signal a2))))
(after-delay or-gate-delay
(lambda ()
set-signal! output new-value)
)
)
)
(define (logical-or s1 s2)
(cond ((or (= s1 1) (= s2 1)) 1)
((and (= s1 0) (= s2 0)) 0)
(else (error "Invalid signal" s)))
(add-action! a1 or-action-procedure)
(add-action! a2 or-action-procedure)
'ok
)
;3.29
(define (compound-or-gate a1 a2 output)
(let ((a1-invert make-wire)
(a2-invert make-wire)
(and-out make-wire))
(inverter a1 a1-invert)
(inverter a2 a2-invert)
(and-gate a1-invert a2-invert and-out)
(inverter and-out output)
'ok
)
)
;3.33
(define (averager a1 a2 mean)
(let ((total (make-connector))
(half (make-connector)))
(adder a1 a2 total)
(multiplier total half mean)
(constant 0.5 half)
'ok)
)
;3.34 Louis's 'squarer' constraint device has two problems: The lesser problem is that square does not have an inverse.
;That is, the constraint device would have to make a choice between the positive and negative square roots. The major problem
;is that, on propagating from an input square to the root, both roots would be unfilled, and thus appear indeterminate
;3.35
(define (squarer a b)
(define (process-new-value)
(if (has-value? b)
(if (< (get-value b) 0)
(error "square less than 0 -- SQUARER" (get-value b))
(set-value! a sqrt(b) me))
(if (has-value? a)
(set-value! b (* a a) me)
'done)
)
)
(define (process-forget-value)
(forget-value! a me)
(forget-value! b me)
(process-new-value)
)
(define (me request)
(cond ((eq? request 'I-have-a-value)
(process-new-value))
((eq? request 'I-lost-my-value)
(process-forget-value))
(else (error "Unknonw request -- SQUARER" request))
)
)
(connect a me)
(connect b me)
me
)
;;3.37 Constraint device operations
(define (cv x)
(define connector (make-connector))
(constant x connector)
connector
)
(define (c* x y)
(let ((z (make-connector)))
(multiplier x y z)
z))