2ca8ede6e39b7180dba7373f8e9dd551caf8b036
[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 <OL start=8>
31 <LI>Define a `neg` operator that negates `true` and `false`.
32
33 Expected behavior: 
34
35     (((neg true) 10) 20)
36
37 evaluates to 20, and 
38
39     (((neg false) 10) 20)
40
41 evaluates to 10.
42
43 <LI>Define an `or` operator.
44
45 <LI>Define an `xor` operator. If you haven't seen this term before, here's a truth table:
46
47     true xor true == false
48     true xor false == true
49     false xor true == true
50     false xor false == false
51
52 </OL>
53
54
55
56 Triples
57 -------
58
59 Recall our definitions of ordered triples.
60
61 >   the triple **(**a**,**b**, **c**)** is defined to be `\f. f a b c`
62
63 To extract the first element of a triple t, you write:
64
65         t (\fst snd trd. fst)
66
67 Here are some definitions in Racket:
68
69         (define make-triple (lambda (fst) (lambda (snd) (lambda (trd) (lambda (f) (((f fst) snd) trd))))))
70         (define fst_of_three (lambda (fst) (lambda (snd) (lambda (trd) fst))))
71         (define snd_of_three (lambda (fst) (lambda (snd) (lambda (trd) snd))))
72
73 Now we can write:
74
75         (define t (((make-triple 10) 20) 30))
76         (t fst_of_three)  ; will evaluate to 10
77         (t snd_of_three)  ; will evaluate to 20
78
79 If you're puzzled by having the triple to the left and the function that
80 operates on it come second, think about why it's being done this way: the pair
81 is a package that takes a function for operating on its elements *as an
82 argument*, and returns *the result of* operating on its elements with that
83 function. In other words, the triple is a higher-order function.
84
85
86 <OL start=13>
87 <LI>Define the `swap12` function that permutes the elements of a triple. Expected behavior:
88
89         (define t (((make-triple 10) 20) 30))
90         ((t swap12) fst_of_three) ; evaluates to 20
91         ((t swap12) snd_of_three) ; evaluates to 10
92
93 Write out the definition of `swap12` in Racket.
94
95
96 <LI>Define a `dup3` function that duplicates its argument to form a triple
97 whose elements are the same.
98 Expected behavior:
99
100         ((dup3 10) fst_of_three) ; evaluates to 10
101         ((dup3 10) snd_of_three) ; evaluates to 10
102
103 <LI>Define a `dup27` function that makes
104 twenty-seven copies of its argument (and stores them in a data structure of
105 your choice).
106
107 </OL>
108
109