-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIII-abstraction-17.rkt
220 lines (169 loc) · 4.57 KB
/
III-abstraction-17.rkt
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
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname III-abstraction-ch17) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #t #t none #f () #f)))
(require test-engine/racket-tests)
;;;; 17 Namesless Functions
(require 2htdp/image)
(require 2htdp/universe)
(define-struct ir
[name price])
; An IR is a structure:
; (make-IR String Number)
; An Inventory is one of:
; – '()
; – (cons IR Inventory)
; [Listof IR] Number -> Boolean
;; (define (find l th)
;; (local (; IR -> Boolean
;; (define (acceptable? ir)
;; (<= (ir-price ir) th)))
;; (filter acceptable? l)))
; [Listof IR] Number -> Boolean
(define (find l th)
(filter (lambda (ir) (<= (ir-price ir) th)) l))
;;;; 17.1 Functions from lambda
;; (lambda (variable-1 ... variable-N) expression)
(check-expect ((lambda (x) (expt 10 x)) 2)
100)
(check-expect
((lambda (name rst) (string-append name ", " rst)) "Robby" "etc.")
"Robby, etc.")
(check-expect
((lambda (ir) (<= (ir-price ir) th))
(make-ir "bear" 10))
#true)
(define th 20)
(check-expect
(map (lambda (x) (expt 10 x)) '(1 2 3))
(list 10 100 1000))
(check-expect
(foldl (lambda (name rst)
(string-append name ", " rst))
"etc."
'("Matthew" "Robby"))
"Robby, Matthew, etc.")
(check-expect
(filter (lambda (ir) (<= (ir-price ir) th))
(list (make-ir "bear" 10)
(make-ir "doll" 33)))
(list (make-ir "bear" 10)))
;; Exercise 279
;; Valid
; (lambda (x y) (x y y))
;; Validity depends on language pack selected
; (lambda () 10)
;; Valid - identity
; (lambda (x) x)
;; Valid
; (lambda (x y) x)
;; Invalid - not well-formed
(check-error
(lambda x 10))
;; Exercise 280
(check-expect
((lambda (x y) (+ x (* x y)))
1 2)
3)
;; Intermediate Student with Lambda gives:
;; define: not allowed in an expression context
;; but works ok on Advanced Student.
;; (kind of like earlier local expressions were off by one section)
(check-expect
((lambda (x y)
(+ x
(local ((define z (* y y)))
(+ (* 3 z) (/ 1 x)))))
1 2)
14)
(check-expect
((lambda (x y)
(+ x
((lambda (z)
(+ (* 3 z) (/ 1 z)))
(* y y))))
1 2)
13.25)
;; Exercise 281
; 1. consumes a number and decides whether it is less than 10;
(check-expect
((lambda (num) (< num 10)) 5)
#true)
; 2. multiplies two given numbers and turns the result into a string;
(check-expect
((lambda (x y) (string-append (number->string (* x y)) ".")) 2 4)
"8.")
; 3. consumes a natural number and returns 0 for evens and 1 for odds;
(check-expect
((lambda (nat) (if (= 0 (modulo nat 2)) 0 1)) 6)
0)
; 4. consumes two inventory records and compares them by price;
(check-expect
((lambda (ir1 ir2) (<= (ir-price ir1) (ir-price ir2)))
(make-ir "name1" 12) (make-ir "name2" 15))
#true)
; 5. adds a red dot at a given Posn to a given Image.
(define emt
(empty-scene 100 100))
(define dot
(circle 3 "solid" "red"))
(check-expect
(place-image
dot
5 5
emt)
((lambda (p img)
(place-image dot (posn-x p) (posn-y p) img))
(make-posn 5 5) emt))
;;;; 17.2 Computing with lambda
;; Exercise 282
(define (f-plain x)
(* 10 x))
(define f-lambda
(lambda (x)
(* 10 x)))
; Number -> Boolean
(define (compare x)
(= (f-plain x) (f-lambda x)))
(check-expect
(compare (random 100000))
#true)
(check-expect
(f-plain (f-plain 42))
4200)
(check-expect
(f-lambda (f-lambda 42))
((lambda (x) (* 10 x))
((lambda (x) (* 10 x))
42)))
(check-expect
((lambda (x) (* 10 x)) 2)
(* 10 2))
(check-expect
((lambda (name rst) (string-append name ", " rst)) "Robby" "etc.")
(string-append "Robby" ", " "etc."))
(check-expect
((lambda (ir) (<= (ir-price ir) th)) (make-ir "bear" 10))
(<= (ir-price (make-ir "bear" 10)) th))
;; Exercise 283
(check-expect
(map (lambda (x) (* 10 x))
'(1 2 3))
(list (* 10 1) (* 10 2) (* 10 3)))
(check-expect
(foldl (lambda (name rst)
(string-append name ", " rst))
"etc."
'("Matthew" "Robby"))
"Robby, Matthew, etc.")
(check-expect
(filter (lambda (ir) (<= (ir-price ir) th))
(list (make-ir "bear" 10)
(make-ir "doll" 33)))
(list (make-ir "bear" 10)))
;; Exercise 284
;; ((lambda (x) x) (lambda (x) x))
;; ((lambda (x) (x x)) (lambda (x) x))
;; ((lambda (x) (x x)) (lambda (x) (x x))) ; recurses infinitely
;; end 17.2?
"end"
(test);; DrRacket does something like this behind the scenes when run via IDE