refine hidden assignment2
[lambda.git] / topics / _assignment2.mdwn
1 Reduction
2 ---------
3
4 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").
5
6 1. `(\x \y. y x) z`
7 2. `(\x (x x)) z`
8 3. `(\x (\x x)) z`
9 4. `(\x (\z x)) z`
10 5. `(\x (x (\y y))) (\z (z z))`
11 6. `(\x (x x)) (\x (x x))`
12 7. `(\x (x x x)) (\x (x x x))`
13
14
15 Booleans
16 --------
17
18 Recall our definitions of true and false.
19
20 >   **true** is defined to be `\t f. t`  
21 >   **false** is defined to be `\t f. f`
22
23 In Racket, these functions can be defined like this:
24
25     (define true (lambda (t) (lambda (f) t)))
26     (define false (lambda (t) (lambda (f) f)))
27
28 (Note that they are different from Racket's *primitive* boolean values `#t` and `#f`.)
29
30 <small>(I know the numbering of the homework problems will restart instead of continuing with 8, 9, ... 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.)</small>
31
32 8.  Define a `neg` operator that negates `true` and `false`.
33
34     Expected behavior: 
35
36         (((neg true) 10) 20)
37
38     evaluates to 20, and 
39
40         (((neg false) 10) 20)
41
42     evaluates to 10.
43
44 9.  Define an `or` operator.
45
46 10. Define an `xor` operator. If you haven't seen this term before, here's a truth table:
47
48         true   xor  true   == false
49         true   xor  false  == true
50         false  xor  true   == true
51         false  xor  false  == false
52
53
54
55 Triples
56 -------
57
58 Recall our definitions of ordered triples.
59
60 >   the triple **(**a**,**b**, **c**)** is defined to be `\f. f a b c`
61
62 To extract the first element of a triple t, you write:
63
64     t (\fst snd trd. fst)
65
66 Here are some definitions in Racket:
67
68     (define make-triple (lambda (fst) (lambda (snd) (lambda (trd) (lambda (f) (((f fst) snd) trd))))))
69     (define fst_of_three (lambda (fst) (lambda (snd) (lambda (trd) fst))))
70     (define snd_of_three (lambda (fst) (lambda (snd) (lambda (trd) snd))))
71
72 Now we can write:
73
74     (define t (((make-triple 10) 20) 30))
75     (t fst_of_three)  ; will evaluate to 10
76     (t snd_of_three)  ; will evaluate to 20
77
78 If you're puzzled by having the triple to the left and the function that
79 operates on it come second, think about why it's being done this way: the pair
80 is a package that takes a function for operating on its elements *as an
81 argument*, and returns *the result of* operating on its elements with that
82 function. In other words, the triple is a higher-order function.
83
84
85 11. Define the `swap12` function that permutes the elements of a triple. Expected behavior:
86
87         (define t (((make-triple 10) 20) 30))
88         ((t swap12) fst_of_three) ; evaluates to 20
89         ((t swap12) snd_of_three) ; evaluates to 10
90
91     Write out the definition of `swap12` in Racket.
92
93
94 12. Define a `dup3` function that duplicates its argument to form a triple
95 whose elements are the same.  Expected behavior:
96
97         ((dup3 10) fst_of_three) ; evaluates to 10
98         ((dup3 10) snd_of_three) ; evaluates to 10
99
100 13. Define a `dup27` function that makes
101 twenty-seven copies of its argument (and stores them in a data structure of
102 your choice).
103
104