tweak assignment
[lambda.git] / exercises / assignment2.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 2.  `v w (\x y. v x)`
8 3.  `(\x y. x) u v`
9 4.  `w (\x y z. x z (y z)) u v`
10
11 Mark all occurrences of `(x y)` in the following terms:
12
13 <small>(I know the numbering of the homework problems will restart instead of continuing with 5, 6, 7, ... It's too much of a pain to fix it right now. We'll put in a better rendering engine later that will make this work right without laborious work-arounds on our part. Please just renumber the problems appropriately)</small>
14
15 5.  `(\x y. x y) x y`
16 6.  `(\x y. x y) (x y)`
17 7.  `\x y. x y (x y)`
18
19 Reduction
20 ---------
21
22 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").
23
24 8.  `(\x \y. y x) z`
25 9.  `(\x (x x)) z`
26 10. `(\x (\x x)) z`
27 11. `(\x (\z x)) z`
28 12. `(\x (x (\y y))) (\z (z z))`
29 13. `(\x (x x)) (\x (x x))`
30 14. `(\x (x x x)) (\x (x x x))`
31
32
33
34 Booleans
35 --------
36
37 Recall our definitions of true and false.
38
39 >   **true** is defined to be `\t f. t`  
40 >   **false** is defined to be `\t f. f`
41
42 In Racket, these functions can be defined like this:
43
44     (define true (lambda (t) (lambda (f) t)))
45     (define false (lambda (t) (lambda (f) f)))
46
47 (Note that they are different from Racket's *primitive* boolean values `#t` and `#f`.)
48
49
50 15. Define a `neg` operator that negates `true` and `false`.
51
52     Expected behavior: 
53
54         (((neg true) 10) 20)
55
56     evaluates to `20`, and 
57
58         (((neg false) 10) 20)
59
60     evaluates to `10`.
61
62 16. Define an `or` operator.
63
64 17. Define an `xor` operator. If you haven't seen this term before, here's a truth table:
65
66         true   xor  true   == false
67         true   xor  false  == true
68         false  xor  true   == true
69         false  xor  false  == false
70
71
72
73 Triples
74 -------
75
76 Recall our definitions of ordered triples.
77
78 >   the triple **(**a**, **b**, **c**)** is defined to be `\f. f a b c`
79
80 To extract the first element of a triple `t`, you write:
81
82     t (\fst snd trd. fst)
83
84 Here are some definitions in Racket:
85
86     (define make-triple  (lambda (fst) (lambda (snd) (lambda (trd) (lambda (f) (((f fst) snd) trd))))))
87     (define fst_of_three (lambda (fst) (lambda (snd) (lambda (trd) fst))))
88     (define snd_of_three (lambda (fst) (lambda (snd) (lambda (trd) snd))))
89
90 Now we can write:
91
92     (define t (((make-triple 10) 20) 30))
93     (t fst_of_three)  ; will evaluate to 10
94     (t snd_of_three)  ; will evaluate to 20
95
96 If you're puzzled by having the triple to the left and the function that
97 operates on it come second, think about why it's being done this way: the pair
98 is a package that takes a function for operating on its elements *as an
99 argument*, and returns *the result of* operating on its elements with that
100 function. In other words, the triple is a higher-order function.
101
102
103 18. Define the `swap12` function that permutes the elements of a triple. Expected behavior:
104
105         (define t (((make-triple 10) 20) 30))
106         ((t swap12) fst_of_three) ; evaluates to 20
107         ((t swap12) snd_of_three) ; evaluates to 10
108
109     Write out the definition of `swap12` in Racket.
110
111
112 19. Define a `dup3` function that duplicates its argument to form a triple
113 whose elements are the same.  Expected behavior:
114
115         ((dup3 10) fst_of_three) ; evaluates to 10
116         ((dup3 10) snd_of_three) ; evaluates to 10
117
118 20. Define a `dup27` function that makes
119 twenty-seven copies of its argument (and stores them in a data structure of
120 your choice).
121
122
123 Folds and Lists
124 ---------------
125
126 21. Using Kapulet syntax, define `fold_left`.
127
128 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`!
129
130 23. Using Kapulet syntax, define `&&` in terms of `fold_right`. (To avoid trickiness about the infix syntax, just call it `append`.)
131
132 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 `'error`.
133
134 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`?
135
136     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.
137
138     Even if you don't get the 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.
139
140
141 Numbers
142 -------
143
144 26. Given that we've agreed to Church's encoding of the numbers:
145
146     <code>0 &equiv; \f z. z</code>  
147     <code>1 &equiv; \f z. f z</code>  
148     <code>2 &equiv; \f z. f (f z)</code>  
149     <code>3 &equiv; \f z. f (f (f z))</code>  
150     <code>...</code>
151
152     How would you express the `succ` function in the Lambda Calculus?