-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecise2.rkt
More file actions
executable file
·325 lines (219 loc) · 9.75 KB
/
Copy pathexecise2.rkt
File metadata and controls
executable file
·325 lines (219 loc) · 9.75 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
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
;; 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-intermediate-lambda-reader.ss" "lang")((modname 2019W2-MT2-starter) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
(require 2htdp/image)
(require spd/tags)
(@assignment mt-2)
(@cwl ???) ;<<< replace ??? with your cwl.
(@problem 1)
;;
;; Write the signature for the following abstract function.
;; Do your SCRATCH WORK SOMEWHERE ELSE, and then
;; put your FINAL ANSWER in the @signature ABOVE the function definition.
;;
;; Uncomment the following line and put your signature in the tag as usual.
;(@signature )
(define (bar p q r s a b)
(cond [(zero? a) r]
[(empty? b) s]
[else
(q (p a (first b))
(bar p q r s (sub1 a) (rest b)))]))
;; ---- PLEASE REMEMBER TO USE SPD HANDIN ABOUT EVERY 20 MINUTES ----
(@problem 2)
;;
;; Consider the following data definition and fold function
;;
(@htdd Dir)
(define-struct dir (name sub-dirs images))
;; Dir is (make-dir String (listof Dir) (listof Image))
;; interp. a directory in the organizer, with a name, a list
;; of sub-dirs and a list of images.
(define I1 (square 20 "solid" "red"))
(define I2 (square 15 "solid" "green"))
(define I3 (square 10 "solid" "blue"))
(define I4 (square 25 "solid" "black"))
(define I5 (square 30 "solid" "purple"))
(define D4 (make-dir "D4" empty (list I1 I2)))
(define D5 (make-dir "D5" empty (list I3)))
(define D6 (make-dir "D6" (list D4 D5) (list I4 I5)))
#;
(define (fn-for-dir d)
(local [(define (fn-for-dir d)
(... (dir-name d)
(fn-for-lod (dir-sub-dirs d))
(fn-for-loi (dir-images d))))
(define (fn-for-lod lod)
(cond [(empty? lod) (...)]
[else
(... (fn-for-dir (first lod))
(fn-for-lod (rest lod)))]))
(define (fn-for-loi loi)
(cond [(empty? loi) (...)]
[else
(... (first loi)
(fn-for-loi (rest loi)))]))]
(fn-for-dir d)))
(@htdf fold-dir)
(@signature (String Y Z -> X) (X Y -> Y) (Image Z -> Z) Y Z -> X)
;; abstract fold for Dir
(check-expect (fold-dir make-dir cons cons empty empty D6) D6)
(@template Dir (listof Dir) (listof Image) encapsulated)
(define (fold-dir c1 c2 c3 b1 b2 d)
(local [(define (fn-for-dir d)
(c1 (dir-name d)
(fn-for-lod (dir-sub-dirs d))
(fn-for-loi (dir-images d))))
(define (fn-for-lod lod)
(cond [(empty? lod) b1]
[else
(c2 (fn-for-dir (first lod))
(fn-for-lod (rest lod)))]))
(define (fn-for-loi loi)
(cond [(empty? loi) b2]
[else
(c3 (first loi)
(fn-for-loi (rest loi)))]))]
(fn-for-dir d)))
;;
;; Complete the design of the function below.
;; The body of your function MUST USE the abstract fold function for Dir.
;; Solutions that do not use fold-dir will NOT be graded.
(@htdf count-imgs-with-height>n)
(@signature Dir Natural -> Natural)
;; produce the count of images in dir with image-height > n
(check-expect (count-imgs-with-height>n D5 10) 0)
(check-expect (count-imgs-with-height>n D6 15) 3)
(check-expect (count-imgs-with-height>n D6 20) 2)
(define (count-imgs-with-height>n dir n) 0) ;stub
;; ---- PLEASE REMEMBER TO USE SPD HANDIN ABOUT EVERY 20 MINUTES ----
(@problem 3)
;;
;; Complete the design for a function called pyramid that consumes a natural
;; number and produces a pyramid of circles that high.
;;
;; The body of your function definition MUST USE built-in abstract functions.
;; Solutions which use part of the list template will NOT be graded
;;
;; Make sure that you take some time to understand the given check-expects.
;;
(@htdf pyramid)
(@signature Natural -> Image)
;; produce a pyramid of n circles
(check-expect (pyramid 0) empty-image)
(check-expect (pyramid 3) (above (circle 10 "solid" "black")
(above (beside (circle 10 "solid" "black")
(circle 10 "solid" "black"))
(beside (circle 10 "solid" "black")
(circle 10 "solid" "black")
(circle 10 "solid" "black")))))
(define (pyramid n) empty-image) ; stub
;; ---- PLEASE REMEMBER TO USE SPD HANDIN ABOUT EVERY 20 MINUTES ----
(@problem 4)
;;
;; You are asked to implement the four abstract list functions andmap, ormap,
;; map, and filter. Because foldr is the abstract function for the (listof X)
;; template itself, you know that some of these could be implemented using
;; foldr.
;;
;; For each of the 4 function definitions below, you should do one and only
;; one of two things:
;; - Code the body of the function so that it calls foldr. Your
;; implementation using foldr must have the complete correct behaviour
;; of the built-in you are implementing.
;;
;; - Leave the function definition as is, but write a comment above the
;; function explaining why calling foldr will not work properly.
;;
(@htdf andmap2)
(@signature (X -> Boolean) (listof X) -> Boolean)
;; produce false as soon as p produces false for one element of lox
(check-expect (andmap2 odd? empty) true)
(check-expect (andmap2 odd? (list 1 3)) true)
(check-expect (andmap2 even? (list 1 2 3)) false)
(@template use-abstract-fn)
;; Either revise the function definition below to call foldr, OR leave
;; the function definition as is and put a comment above the function
;; definition explaining why calling foldr will not work properly.
(define (andmap2 p lox) false)
(@htdf ormap2)
(@signature (X -> Boolean) (listof X) -> Boolean)
;; produce true as soon as p produces true for one element of lox
(check-expect (ormap2 odd? empty) false)
(check-expect (ormap2 even? (list 1 3)) false)
(check-expect (ormap2 even? (list 1 2 3)) true)
(@template use-abstract-fn)
;; Either revise the function definition below to call foldr, OR leave
;; the function definition as is and put a comment above the function
;; definition explaining why calling foldr will not work properly.
(define (ormap2 p lox) false)
(@htdf filter2)
(@signature (X -> Boolean) (listof X) -> (listof X))
;; produce list containing only those items for which p produces true
(check-expect (filter2 positive? empty) empty)
(check-expect (filter2 negative? (list 1 -2 3 -4)) (list -2 -4))
(check-expect (filter2 positive? (list 1 -2 3 -4)) (list 1 3))
(@template use-abstract-fn)
;; Either revise the function definition below to call foldr, OR leave
;; the function definition as is and put a comment above the function
;; definition explaining why calling foldr will not work properly.
(define (filter2 p lox) empty)
(@htdf map2)
(@signature (X -> Y) (listof X) -> (listof Y))
;; given fn and (list n0 n1 ...) produce (list (fn n0) (fn n1) ...)
(check-expect (map2 sqr empty) empty)
(check-expect (map2 sqr (list 2 4)) (list 4 16))
(check-expect (map2 sqrt (list 16 9)) (list 4 3))
(check-expect (map2 abs (list 2 -3 4)) (list 2 3 4))
(check-expect (map2 string-length (list "a" "bc" "def")) (list 1 2 3))
(@template use-abstract-fn)
;; Either revise the function definition below to call foldr, OR leave
;; the function definition as is and put a comment above the function
;; definition explaining why calling foldr will not work properly.
(define (map2 p lox) empty)
;; ---- PLEASE REMEMBER TO USE SPD HANDIN ABOUT EVERY 20 MINUTES ----
(@problem 5)
;;
;; Design a function called group that consumes a list of numbers and groups
;; numbers that are the same into ordered sublists. For example:
;;
;; (group (list 4 1 6 1 6)) --> (list (list 1 1) (list 4) (list 6 6))
;;
;; IMPORTANT: Your design must include all relevant design elements.
;;
;; NOTE: We have provided you with the list-min helper, which was useful in
;; our solution.
;;
;(@htdf group) ;uncomment this line when you start
(@signature (listof Number) -> Number)
;; produce minimum number in list
;; CONSTRAINT: lon must have at least one element
(check-expect (list-min (list 1)) 1)
(check-expect (list-min (list 0)) 0)
(check-expect (list-min (list 3 2 1 5 9 0)) 0)
(@template use-abstract-fn)
(define (list-min lon)
(foldr min (first lon) (rest lon)))
;; ---- PLEASE REMEMBER TO USE SPD HANDIN ABOUT EVERY 20 MINUTES ----
(@problem 6)
;;
;; Design a function called sq-frac that consumes a positive number and
;; produces a fractal image like the ones shown in the exam supplement.
;;
;; To save writing we are giving you a simple function called sq that consumes
;; a number and produces a black solid square of that side-length. This will
;; save you having to write "solid" "black" multiple times.
;;
;; You must include all relevant design recipe elements, including a three part
;; termination argument.
;;
(define CUTOFF 10)
;(@htdf sq-frac) ; Uncomment this line and complete the design of sq-frac.
(@htdf sq)
(@signature Number -> Image)
;; produce solid black square of given side-length
(check-expect (sq 10) (square 10 "solid" "black"))
(check-expect (sq 20) (square 20 "solid" "black"))
(@template Number)
(define (sq s)
(square s "solid" "black"))