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 | ;;; Time-stamp: <2008-05-10 03:27:31 vishal final-review.scm>
;;;
;;; CS3S (Unofficial) Final Review Problems
;;; Spring 2008
;;; UC Berkeley
;;;
;;; Please keep a Scheme interpreter handy when trying the following
;;; exercises. You should be able to edit this document in a text
;;; editor and copy/paste pieces of code into the interpreter as you
;;; go along. Also note that this review sheet by no means covers
;;; everything you need to know for the final. If you have any
;;; questions, feel that something integral is missing, or have a
;;; newfound appreciation for Scheme after completing this worksheet,
;;; please email us at <selfpace@cs.berkeley.edu>.
;;; Round 1: Simple Functions
;; 1. Evaluate the following expressions. If an expression results in an
;; error, specify the reason.
(- 100 10)
(- 3 1 2)
(- 8)
(-8)
(- -8)
(1 - 8)
(1 -8)
(+ 5)
(quotient 5 2)
(remainder 5 2)
(quotient 2 5)
(remainder 2 5)
(sqrt 4)
(sqrt 2 + 2)
;; 2. Suggest better names for the following functions:
(define (fn1 a b)
(+ a b a b))
(define (fn2 a b c)
(= a b c))
(define (fn3 a b)
(sqrt (+ (* a a) (* b b))))
;; 3. Which of the following functions differ in output from fn1 given
;; the same input?
(define (fn1-wannabe1 a b)
(+ a a b b))
(define (fn1-wannabe2 a b)
(+ (* 2 a) (* b 2)))
(define (fn1-wannabe3 a b)
(+ (- b a) a (+ b a) a))
;;; Round 2: Lists
;; Fill in the blanks with car, cdr, cons, or list to get the desired
;; results (denoted by '=>'):
(____ (____ (____ '(1 (2 (3))) )))
; => (3)
(____ (____ '(1 (2 (3))) ))))
; => 2
(____ 1 '())
; => (1)
(____ (____ 1 '(2 3)))
; => 1
(____ 1 (____ (____ 2 (____ (____ 3 '())))))
; => (1 (2 (3)))
(append (____ 1) (____ 2) (____ 3))
; => (1 2 3)
(____ '(1) '(2 3))
; => (1 2 3)
(____ '(1) '(2 3))
; => ((1) 2 3)
;;; Round 3: Evaluation
;; Evaluate the following calls made to the functions defined earlier:
(fn1 3 3)
; =>
(fn1 4 9)
; =>
(fn2 1 2 3)
; =>
(fn2 1 1 1)
; =>
(fn3 3 4)
; =>
(fn3 5 (fn1 3 3))
; => (fn3 5 _________) ; Simplify the above call first
; => ; Then evaluate
;;; Round 4: Predicates
;; 1. Complete the definitions of the following functions. Some may ask
;; you to define them multiple ways.
;; (even? 3) => #f
;; (even? 2) => #t
(define (even? n)
)
;; (odd? 3) => #t
;; (odd? 2) => #f
(define (odd? n) ; don't use even? to define this version
)
(define (odd? n) ; use even? to define this version
)
;; 2. Describe what the following function does and how it works:
(define (all-equal? a b c)
(and (equal? a b)
(equal? b c)))
;;; Round 5: Case Analysis
;; 1. Define a function named majority that accepts 3 arguments and
;; returns #t if 2 or more of its arguments are #t, and #f otherwise.
;; (majority #f #f #f) => #f
;; (majority #f #t #f) => #f
;; (majority #t #f #t) => #t
(define (majority a b c)
)
;; 2. Define a function named same-suit that accepts three cards of the
;; form (symbol suit) and returns #t if the three cards all have the
;; same suit, and #f otherwise.
;; (same-suit? '(K H) '(10 H) '(J H)) => #t
;; (same-suit? '(Q S) '( 8 C) '(5 D)) => #f
(define (same-suit? card1 card2 card3)
)
;; 3. Define a function named ordinal that works as follows:
;; (ordinal 1) => st
;; (ordinal 2) => nd
;; (ordinal 3) => rd
;; (ordinal 4) => th
;; (ordinal 1329) => th
(define (ordinal n)
)
;; 4. Using the ordinal function above, define another function called
;; date-format that works as follows:
;; (date-format 21 'september 1987)
;; => (the 21 st of september in the year 1987)
;; (date-format 29 'october 1900)
;; => (the 29 th of october in the year 1900)
(define (date-format date month year)
)
;;; Round 7: Simple Recursionn
;; 1. Define a function named duplicate-elements that takes a list of
;; elements and returns a list with those elements appearing twice
;; consecutively.
;; (duplicate-elements '(1 2 3)) => '(1 1 2 2 3 3)
(define (duplicate-elements L)
)
;; 2. Define a function named all-sums that takes a list of numbers and
;; returns a list of sums of all sublists starting from the first
;; element. The resulting list should be the same length as the
;; original list. You may also define a helper function or three.
;; (all-sums '(1 2 3)) => (1 3 6)
;; (all-sums '(0 1 1 2 3)) => (0 1 2 4 7)
(define (all-sums L)
)
;; 3. What's wrong with the following function, which accepts a queue
;; of people is supposed to return the position of the person in the
;; queue?:
;; (position-of 'john '(john jacob joel)) => 0
;; (position-of 'john '(jack jake john)) => 2
(define (position-of name queue)
(if (equal? name (car queue)) 0
(+ 1 (position-of name (cdr queue)))))
;; 4. Define a function named zip that accepts two equal-sized lists
;; of elements and returns a single list of pairs of corresponding
;; elements from the original lists.
;; (zip '(1 2 3) '(4 5 6)) => ((1 4) (2 5) (3 6))
;; (zip '(peter xavier) '(23 39)) => ((peter 23) (xavier 39))
(define (zip L1 L2)
)
;; 5. (skip if you hate math) The 3n+1 Conjecture
;; See http://en.wikipedia.org/wiki/Collatz_conjecture
;; a. Define a function named collatz that acts as follows:
;; ( n/2 if n is even
;; collatz(n) = {
;; ( 3n+1 if n is odd
;; Feel free to use any functions you defined earlier.
(define (collatz n)
)
;; b. Define a function named collatz-sequence that collects the
;; decreasing sequence of Collatz numbers (until 1 is reached) as
;; follows:
;; (collatz-sequence 3) => (10 5 16 8 4 2 1)
;; (collatz-sequence 28) => (14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1)
(define (collatz-sequence n)
)
;;; Round 8: More Recursionn
;; 1. What does the following function do?:
(define (loves-me? n)
(if (= n 1) #t
(not (loves-me (- n 1)))))
;; 2. Expand the following call into a series of recursive calls along
;; with the result:
(loves-me? 4)
;; Here's something to start you off:
;; => (not (loves-me? 3))
;;
;; ...
;;
;; => ___ (this should probably be #t or #f)
;; That last problem wasn't too realistic. Let's try this again.
;; First, here's a function to make a flower of a given number of
;; petals (the stem comes for free!):
;; (make-flower 3) => (petal petal petal stem)
;; (make-flower 1) => (petal stem)
;; (make-flower 0) => (stem)
(define (make-flower num-petals)
(if (= num-petals 0) '(stem)
(cons 'petal (make-flower (- num-petals 1)))))
;; This function determines whether a flower in the above form has any
;; petals left on it or not.
;; (has-petals? '(stem)) => #f
;; (has-petals? '(petal petal stem)) => #t
(define (has-petals? flower)
(or (null? flower)
(equal? (car flower) 'petal)))
;; Feel free to play around with these functions before continuing!
;; 3. This next one you can define by yourself:
;; (pluck '(petal petal petal stem)) => (petal petal stem)
;; (pluck '(petal stem)) => (stem)
;; (pluck '(stem)) => (stem)
(define (pluck flower)
)
;; 4. Now that we've got actual petals to pluck, pluck away by
;; redefining loves-me? to accomodate this change. You should use the
;; functions we just defined. I've started you off with some code:
(define (loves-me? flower)
(if (not (has-petals? flower)) _____
(not (loves-me? (_____ flower)))))
;; 5. Expand the following call into a series of recursive calls along
;; with the result:
(loves-me? (make-flower 4))
;;; Round 9: Functionals (FINAL ROUND)
;; From here on out, use higher-order functions to accomplish what you
;; may have with recursion. Many of the functions you will be defining
;; can be defined multiple ways, so just pick one (or if you want the
;; practice, try them all).
;; 1. Define a function named sum that accepts a list of numbers
;; and returns their sum.
;; (sum '(1 2 3)) => 6
;; (sum '()) => 0
(define (sum L)
)
;; 2. Define a function named product that accepts a list of numbers
;; and returns their product.
;; (product '(1 2 3)) => 6
;; (product '()) => 1
(define (product L)
)
;; 3. Define a function named sum-squares that accepts a list of numbers
;; and returns the sum of their squares.
;; (sum-squares '(1 2 3)) => 14
;; (sum-squares '()) => 0
(define (sum-squares L)
)
;; 4. Define a function named all-equal? that accepts a list of atoms and
;; returns #t if they are all equal, and #f otherwise.
;; (all-equal? '(1 2 3)) => #f
;; (all-equal? '(a a a)) => #t
(define (all-equal? L)
)
;; 5. Define a function named same-suit that accepts a list of cards of
;; the form (symbol suit) and returns #t if they all have the same
;; suit, and #f otherwise.
;; (same-suit? '((K H) (10 H) (J H))) => #t
;; (same-suit? '((Q S) ( 8 C) (5 D))) => #f
;; (same-suit? '((1 C))) => #t
(define (same-suit? L)
)
;; 6. Define a function named zip that accepts two equal-sized lists
;; of elements and returns a single list of pairs of corresponding
;; elements from the original lists.
;; (zip '(1 2 3) '(4 5 6)) => ((1 4) (2 5) (3 6))
;; (zip '(peter xavier) '(23 39)) => ((peter 23) (xavier 39))
(define (zip L1 L2)
)
;; 7. Define a function named batting-avg that accepts a list
;; containing hit/miss data and returns the hit:hit attempt ratio.
;; (batting-avg '(hit hit hit)) => 1
;; (batting-avg '(hit hit miss)) => 2/3
;; (batting-avg '(miss miss)) => 0
(define (batting-avg data)
)
;; By now you should be best pals with (or a weary enemy of) map,
;; apply, accumulate find-if, find-if-not, remove-if, keep-if, any,
;; every, lambda, and assoc. If you think they're special, just wait
;; until you write a few of them yourself.
;; 8. Write the following functionals using no other functionals (you can
;; use whatever else you like, especially recursion):
(define (assoc key L)
)
(define (every predicate L)
)
(define (any predicate L)
)
(define (map fn L)
)
(define (find-if fn L)
)
|