f4d67862947e6ef210cb1b9cd9dedba96bb5f07a
[lambda.git] / exercises / assignment2_answers.mdwn
1 Syntax
2 ------
3
4 Insert all the implicit `( )`s and <code>&lambda;</code>s into the following abbreviated expressions.
5
6 1.  `x x (x x x) x`  
7      <code><b>(((</b>x x<b>)</b> (<b>(</b>x x<b>)</b> x)<b>)</b> x<b>)</b></code>
8 2.  `v w (\x y. v x)`  
9     <code><b>((</b>v w<b>)</b> (\x <b>(\</b>y <b>(</b>v x<b>))</b>)<b>)</b></code>
10 3.  `(\x y. x) u v`  
11     <code><b>((</b>(\x <b>(\</b>y x<b>)</b>) u<b>)</b> v<b>)</b></code>
12 4.  `w (\x y z. x z (y z)) u v`  
13     <code><b>(((</b>w (\x <b>(\</b>y <b>(\</b>z <b>((</b>x z<b>)</b> (y z)<b>)))</b>)<b>)</b> u<b>)</b> v<b>)</b></code>
14
15 Mark all occurrences of `(x y)` in the following terms:
16
17 5.  <code>(\x y. <u>x y</u>) x y</code>
18 6.  <code>(\x y. <u>x y</u>) (<u>x y</u>)</code>
19 7.  <code>\x y. <u>x y</u> (<u>x y</u>)</code>
20
21 Reduction
22 ---------
23
24 Find "normal forms" for the following---that is, reduce them until no more reductions are possible. As mentioned in the notes, we'll write <code>&lambda;x</code> as `\x`. If we ever say "reduce" without qualifications, we mean just "beta-reduce" (as opposed to "(beta + eta)-reduce").
25
26 8.  `(\x \y. y x) z` ~~> `\y. y z`
27 9.  `(\x (x x)) z` ~~> `z z`
28 10. `(\x (\x x)) z` ~~> `\x x`
29 11. `(\x (\z x)) z` ~~> `\y z`, be sure to change `\z` to a different variable so as not to "capture" `z`
30 12. `(\x (x (\y y))) (\z (z z))` ~~> `\y y`
31 13. `(\x (x x)) (\x (x x))`  umm..., reductions will forever be possible, they just don't "do" much
32 14. `(\x (x x x)) (\x (x x x))`  that's just mean
33
34
35
36 Booleans
37 --------
38
39 For these questions, and the ones on triples below, we're setting them up so as to encourage you to experiment with Racket and to formulate your answer in Scheme/Racket syntax. But you can answer in Lambda Calculus syntax if you prefer.
40
41 Recall our definitions of true and false.
42
43 >   **true** is defined to be `\t f. t`  
44 >   **false** is defined to be `\t f. f`
45
46 In Racket, these functions can be defined like this:
47
48     (define true (lambda (t) (lambda (f) t)))
49     (define false (lambda (t) (lambda (f) f)))
50
51 (Note that they are different from Racket's *primitive* boolean values `#t` and `#f`.)
52
53
54 15. Define a `neg` operator that negates `true` and `false`.
55
56     Expected behavior: 
57
58         (((neg true) 10) 20)
59
60     evaluates to `20`, and 
61
62         (((neg false) 10) 20)
63
64     evaluates to `10`.
65
66         (define neg (lambda (p) ((p false) true)))
67
68 16. Define an `or` operator.
69
70         (define or (lambda (p) (lambda (q) ((p p) q))))
71
72     or:
73
74         (define or (lambda (p) (lambda (q) ((p true) q))))
75
76
77 17. Define an `xor` operator. If you haven't seen this term before, here's a truth table:
78
79         true   xor  true   == false
80         true   xor  false  == true
81         false  xor  true   == true
82         false  xor  false  == false
83
84     <!-- -->
85
86         (define xor (lambda (p) (lambda (q) ((p (neg q)) q))))
87
88
89 Triples
90 -------
91
92 Recall our definitions of ordered triples.
93
94 >   the triple **(**a**, **b**, **c**)** is defined to be `\f. f a b c`
95
96 To extract the first element of a triple `t`, you write:
97
98     t (\fst snd trd. fst)
99
100 Here are some definitions in Racket:
101
102     (define make-triple  (lambda (fst) (lambda (snd) (lambda (trd) (lambda (f) (((f fst) snd) trd))))))
103     (define fst_of_three (lambda (fst) (lambda (snd) (lambda (trd) fst))))
104     (define snd_of_three (lambda (fst) (lambda (snd) (lambda (trd) snd))))
105
106 Now we can write:
107
108     (define t (((make-triple 10) 20) 30))
109     (t fst_of_three)  ; will evaluate to 10
110     (t snd_of_three)  ; will evaluate to 20
111
112 If you're puzzled by having the triple to the left and the function that
113 operates on it come second, think about why it's being done this way: the triple
114 is a package that takes a function for operating on its elements *as an
115 argument*, and returns *the result of* operating on its elements with that
116 function. In other words, the triple is a higher-order function.
117
118
119 18. Define the `swap12` function that permutes the elements of a triple. Expected behavior:
120
121         (define t (((make-triple 10) 20) 30))
122         ((t swap12) fst_of_three) ; evaluates to 20
123         ((t swap12) snd_of_three) ; evaluates to 10
124
125     Write out the definition of `swap12` in Racket.
126
127         (define swap12 (lambda (x) (lambda (y) (lambda (z)
128                          (lambda (f) (((f y) x) z))))))
129
130
131 19. Define a `dup3` function that duplicates its argument to form a triple
132 whose elements are the same.  Expected behavior:
133
134         ((dup3 10) fst_of_three) ; evaluates to 10
135         ((dup3 10) snd_of_three) ; evaluates to 10
136
137     <!-- -->
138
139         (define dup3 (lambda (x)
140                        (lambda (f) (((f x) x) x))))
141
142 20. Define a `dup27` function that makes
143 twenty-seven copies of its argument (and stores them in a data structure of
144 your choice).
145
146     OK, then we will store them in a triply-nested triple:
147
148         (define dup27 (lambda (x) (dup3 (dup3 (dup3 x)))))
149
150 Folds and Lists
151 ---------------
152
153 21. Using Kapulet syntax, define `fold_left`.
154
155         # fold_left (f, z) [a, b, c] == f (f (f z a) b) c
156         letrec
157           fold_left (f, z) xs = case xs of
158                                   []       then z;
159                                   x' & xs' then fold_left (f, f (z, x')) xs'
160                                 end
161         in fold_left
162
163
164 22. Using Kapulet syntax, define `filter` (problem 7 in last week's homework) in terms of `fold_right` and other primitive syntax like `lambda`, `&`, and `[]`. Don't use `letrec`! All the `letrec`-ing that happens should come from the one inside the definition of `fold_right`.
165
166         let
167           filter (p, xs) = fold_right ((lambda (y, ys). if p y then y & ys else ys), []) xs
168         in filter
169
170 23. Using Kapulet syntax, define `&&` in terms of `fold_right`. (To avoid trickiness about the infix syntax, just call it `append`.) As with problem 22 (the previous problem), don't use `letrec`!
171
172         let
173           xs && ys = fold_right ((&), ys) xs
174           # or append (xs, ys) = ...
175         in (&&)
176
177 24. Using Kapulet syntax, define `head` in terms of `fold_right`. When applied to a non-empty list, it should give us the first element of that list. When applied to an empty list, let's say it should give us `'err`. As with problem 22, don't use `letrec`!
178
179         let
180           head xs = fold_right ((lambda (y, _). y), 'err) xs
181         in head
182
183 25. We mentioned in the Encoding notes that `fold_left (flipped_cons, []) xs` would give us the elements of `xs` but in the reverse order. So that's how we can express `reverse` in terms of `fold_left`. How would you express `reverse` in terms of `fold_right`? As with problem 22, don't use `letrec`!
184
185     This problem does have an elegant and concise solution, but it may be hard for you to figure it out. We think it will a useful exercise for you to try, anyway. We'll give a [[hint|assignment2 hint]]. Don't look at the hint until you've gotten really worked up about the problem. Before that, it probably will just be baffling. If your mind has really gotten its talons into the problem, though, the hint might be just what you need to break it open.
186
187     There are also other, less cool answers. Perhaps you'll find one of them first.
188
189     Even if you don't get any answer, we think the experience of working on the problem, and then understanding the answer when we reveal it, will be satisfying and worthwhile. It also fits our pedagogical purposes for some of the recurring themes of the class.
190
191
192 Numbers
193 -------
194
195 26. Given that we've agreed to Church's encoding of the numbers:
196
197     <code>0 &equiv; \f z. z</code>  
198     <code>1 &equiv; \f z. f z</code>  
199     <code>2 &equiv; \f z. f (f z)</code>  
200     <code>3 &equiv; \f z. f (f (f z))</code>  
201     <code>...</code>
202
203     How would you express the `succ` function in the Lambda Calculus?
204
205         let succ = \n. \f z. f (n f z)        
206