-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch2.rkt-autorec1.ch2
383 lines (307 loc) · 9.18 KB
/
ch2.rkt-autorec1.ch2
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
#lang sicp
(define (square x) (* x x))
(define (neg? x) (< x 0))
(define (same-sign? x y)
(or (not (or (neg? x) (neg? y)))
(and (neg? x) (neg? y))))
(define (average x y) (/ (+ x y) 2))
; Demo - Rational number arithmetic
(define (add-rat x y)
(make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (sub-rat x y)
(make-rat (- (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (mul-rat x y)
(make-rat (* (numer x) (numer y))
(* (denom x) (denom y))))
(define (div-rat x y)
(make-rat (* (numer x) (denom y))
(* (denom x) (numer y))))
(define (equal-rat? x y)
(= (* (numer x) (denom y))
(* (numer y) (denom x))))
; Exercise 2.1 - A better make-rat
;
; Implement make-rat so that it returns a
; numerator and denominator reduce to lowest
; terms, and normalized such that if the rational
; number is negative, the numerator contains the
; "negative" integer value.
(define (make-rat n d)
(let ((g (gcd n d)))
(cond
((same-sign? n d) (cons (/ (abs n) g) (/ (abs d) g)))
((neg? n) (cons (/ n g) (/ d g)))
(else (cons (/ (- n) g) (/ (- d) g))))))
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (print-rat x)
(display (numer x))
(display "/")
(display (denom x))
(newline))
; Exercise 2.2 - Line Segments
(define (midpoint seg)
(let ((a (start-segment seg))
(b (end-segment seg)))
(make-point (average (x-point a) (x-point b))
(average (y-point a) (y-point b)))))
(define (make-segment start end)
(cons start end))
(define (start-segment x) (car x))
(define (end-segment x) (cdr x))
(define (make-point x y) (cons x y))
(define (x-point p) (car p))
(define (y-point p) (cdr p))
(define (print-point p)
(display "(")
(display (x-point p))
(display ",")
(display (y-point p))
(display ")")
(newline))
; 2.3 - Rectangles
; representation 1 (axis-aligned only...)
(define (area rect)
(* (width rect) (height rect)))
(define (perimeter rect)
(+ (* 2 (width rect))
(* 2 (height rect))))
(define (make-rectangle p1 p2)
(cons p1 p2))
(define (width r)
(abs-diff x-point (car r) (cdr r)))
(define (height r)
(abs-diff y-point (car r) (cdr r)))
(define (abs-diff f x y)
(abs (- (f x) (f y))))
; 2.4 - An alternate implementation of pairs
;
; (define (cons x y)
; (lambda (m) (m x y)))
;
; (define (car z)
; (z (lambda (p q) p)))
;
; (define (cdr z)
; (z (lambda (p q) q)))
;
; Here, cons constructs a pair in the form of a
; procedure that accepts a function (m) and applies
; it to its two elements. m is a "retriever" function
; which, by receiving the pair's elements as arguments,
; it able to return either, thus simulating the behaviour
; of car and cdr.
; 2.5 - "Arithmetic" integer pairs
(define (cons-int x y)
(* (expt 2 x) (expt 3 y)))
(define (car-int z)
(if (= (remainder z 2) 0)
(+ 1 (car-int (/ z 2)))
0))
(define (cdr-int z)
(if (= (remainder z 3) 0)
(+ 1 (cdr-int (/ z 3)))
0))
; 2.6 - Church numerals (hmmm)
(define zero (lambda (f) (lambda (x) x)))
(define (add-1 n)
(lambda (f) (lambda (x) (f ((n f) x)))))
; Demo + exercises - Interval arithmetic
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(define (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (div-interval x y)
(if (empty? y)
(error "Cannot divide by interval spanning zero -- " y)
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y))))))
; 2.7 - completing the implementation
(define (make-interval a b) (cons a b))
(define (lower-bound x) (car x))
(define (upper-bound x) (cdr x))
; 2.8 - interval subtraction
(define (sub-interval x y)
(make-interval (- (lower-bound x)
(upper-bound y))
(- (upper-bound x)
(lower-bound y))))
; 2.10 - checking for division by zero
(define (width-interval a)
(/ (- (upper-bound a)
(lower-bound a))
2))
(define (empty? a) (= (width-interval a) 0))
; 2.11 - optimizing mul-interval
;
; Suppose we have two intervals [a, b] and [c, d].
; There are 16 possible arrangments of polarity (-/+)
; between the 4 endpoints. Some of these are invalid
; by the requirement a <= b for any interval [a, b]
; (i.e. no interval can have a positive lower bound and
; a negative upper bound). After eliminating these,
; 9 arrangements remain:
;
; [-,-] [-,-] *
; [-,-] [-,+] *
; [-,-] [+,+] *
; [-,+] [-,-]
; [-,+] [-,+] *
; [-,+] [+,+] *
; [+,+] [-,-]
; [+,+] [-,+]
; [+,+] [+,+] *
;
; (* - sorted interval pairs)
;
; We can actually narrow these cases further by first
; sorting the intervals. Then, the 6 possibilities are:
;
; [-,-] [-,-]
; [-,-] [-,+]
; [-,-] [+,+]
; [-,+] [-,+]
; [-,+] [+,+]
; [+,+] [+,+]
;
; In all but the case [-,+] [-,+], we can find
; the products among endpoints directly by comparing
; a, b, c, and d, such that we only need to do
; two multiplications:
;
; [-,-] [-,-] -> [bd, ac]
; [-,-] [-,+] -> [ad, ac]
; [-,-] [+,+] -> [ad, bc]
; [-,+] [-,-] -> [bc, ac]
; [-,+] [-,+] -> <comnpare all 4 products here>
; [-,+] [+,+] -> [ad, bd]
; [+,+] [-,-] -> [bc, ad]
; [+,+] [-,+] -> [bc, bd]
; [+,+] [+,+] -> [ac, bd]
(define (less-interval x y)
(if (= (lower-bound x) (lower-bound y))
(< (upper-bound x) (upper-bound y))
(< (lower-bound x) (lower-bound y))))
(define (min-interval x y)
(if (less-interval y x) y x))
(define (max-interval x y)
(if (less-interval y x) x y))
(define (mul-interval-fast x y)
(let ((x (min-interval x y))
(y (max-interval x y)))
(let ((a (lower-bound x))
(b (upper-bound x))
(c (lower-bound y))
(d (upper-bound y)))
(cond
((and (neg? a) (neg? b) (neg? c) (neg? d))
(make-interval (* b d) (* a c)))
((and (neg? a) (neg? b) (neg? c) (not (neg? d)))
(make-interval (* a d) (* a c)))
((and (neg? a) (neg? b) (not (neg? c)) (not (neg? d)))
(make-interval (* a d) (* b c)))
((and (neg? a) (not (neg? b)) (neg? c) (not (neg? d)))
(let ((p1 (* a c))
(p2 (* a d))
(p3 (* b c))
(p4 (* b d)))
(make-interval (min p1 p2 p3 p4) (max p1 p2 p3 p4))))
((and (neg? a) (not (neg? b)) (not (neg? c)) (not (neg? d)))
(make-interval (* a d) (* b d)))
((and (not (neg? a)) (not (neg? b)) (not (neg? c)) (not (neg? d)))
(make-interval (* a c) (* b d)))))))
; 2.12 - intervals as midpoint + tolerance
(define (make-center-percent c p)
(let ((width (abs (* c p))))
(make-interval (- c width) (+ c width))))
(define (center a)
(/ (+ (upper-bound a)
(lower-bound a))
2))
(define (percent a)
(/ (width a) (abs (center a))))
; 2.13 thru 2.16 - various mathematics exercises about intervals
; === Hierarchical Structures ===
; Lists
; 2.17 - Last Pair
(define (last-pair x)
(if (null? (cdr x))
x
(last-pair (cdr x))))
; Demo - Appending to a list
(define (append list1 list2)
(if (null? list1)
list2
(cons (car list1) (append (cdr list1) list2))))
; 2.18 - Reversing a list
;
; Can this definition be formulated recursively?
(define (reverse l)
(define (iter l result)
(if (null? l)
result
(iter (cdr l) (cons (car l) result))))
(iter l (list)))
; 2.19 - Making count-change flexible
; recall the demonstration from Chapter 1
(define (count-change amount coins)
(cond
((= amount 0) 1)
((or (< amount 0) (no-more? coins)) 0)
(else (+ (count-change amount
(except-first coins))
(count-change (- amount
(first-denomination coins))
coins)))))
(define no-more? null?)
(define first-denomination car)
(define except-first cdr)
; 2.20 - Variable numbers of arguments
(define (same-parity x . y)
(define (iter l)
(cond
((null? l) nil)
((same-parity? x (car l)) (cons (car l)
(iter (cdr l))))
(else (iter (cdr l)))))
(cons x (iter y)))
(define (same-parity? x y)
(= (remainder x 2) (remainder y 2)))
; 2.21 - The map() primitive
(define (square-list-1 items)
(if (null? items)
nil
(cons (square (car items))
(square-list-1 (cdr items)))))
(define (square-list-2 items)
(map square items))
; 2.22 - for-each
(define (for-each f items)
(cond
((not (null? items))
(f (car items))
(for-each f (cdr items)))))
; 2.27 - deep-reverse
(define (deep-reverse items)
(define (iter x result)
(if (null? x)
result
(let ((elt (car x)))
(iter (cdr x)
(cons (if (list? elt)
(deep-reverse elt)
elt)
result)))))
(iter items nil))
; 2.28 - fringe() (enumerating a tree's leaves)