313db95633e36c857b5e6afe417a9ffb03c33b9c
[lambda.git] / assignment1.mdwn
1 **Reduction**
2
3 Find "normal forms" for the following (that is, reduce them as far as it's possible to reduce 
4 them):
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 Recall our definitions of true and false.
18
19         "true" defined to be `\t \f. t`
20         "false" defined to be `\t \f. f`
21
22 In Racket, these can be defined like this:
23
24         (define true (lambda (t) (lambda (f) t)))
25         (define false (lambda (t) (lambda (f) f)))
26
27 (8). Define a "neg" operator that negates "true" and "false".
28 Expected behavior: (((neg true) 10) 20) evaluates to 20,
29 (((neg false) 10) 20) evaluates to 10.
30
31 (9). Define an "and" operator.
32
33 10. Define an "xor" operator. (If you haven't seen this term before, here's a truth table:
34
35         true xor true = false
36         true xor false = true
37         false xor true = true
38         false xor false = false
39 )
40
41 11. Inspired by our definition of boolean values, propose a data structure
42 capable of representing one of the two values "black" or "white". If we have
43 one of those values, call it a black-or-white-value, we should be able to
44 write:
45
46         the-black-or-white-value if-black if-white
47
48 (where if-black and if-white are anything), and get back one of if-black or
49 if-white, depending on which of the black-or-white values we started with. Give
50 a definition for each of "black" and "white". (Do it in both lambda calculus
51 and also in Racket.)
52
53 12. Now propose a data structure capable of representing one of the three values
54 "red" "green" or "blue," based on the same model. (Do it in both lambda
55 calculus and also in Racket.)
56
57
58
59 Pairs
60 -----
61
62 Recall our definitions of ordered pairs.
63
64         the pair (x,y) is defined as `\f. f x y`
65
66 To extract the first element of a pair p, you write:
67
68         p (\fst \snd. fst)
69
70 Here are some defintions in Racket:
71
72         (define make-pair (lambda (fst) (lambda (snd) (lambda (f) ((f fst) snd)))))
73         (define get-first (lamda (fst) (lambda (snd) fst)))
74         (define get-second (lamda (fst) (lambda (snd) snd)))
75
76 Now we can write:
77         (define p ((make-pair 10) 20))
78         (p get-first)   ; will evaluate to 10
79         (p get-second)  ; will evaluate to 20
80
81 If you're bothered by having the pair to the left and the function that operates on it come seco\
82 nd, think about why it's being done this way: the pair is a package that takes a function for op\
83 erating on its elements as an argument, and returns the result of operating on its elemens with \
84 that function. In other words, the pair is also a function.
85
86 If you like, you can disguise what's going on like this:
87         (define lifted-get-first (lambda (p) (p get-first)))
88         (define lifted-get-second (lambda (p) (p get-second)))
89
90 Now you can write:
91         (lifted-get-first p)
92 instead of:
93         (p get-first)
94 However, the latter is still what's going on under the hood.
95
96
97 13. Define a "swap" function that reverses the elements of a pair.
98 Expected behavior:
99         (define p ((make-pair 10) 20))
100         ((p swap) get-first) ; evaluates to 20
101         ((p swap) get-second) ; evaluates to 10
102
103 Write out the definition of swap in Racket.
104
105
106 14. Define a "dup" function that duplicates its argument to form a pair
107 whose elements are the same.
108 Expected behavior:
109         ((dup 10) get-first) ; evaluates to 10
110         ((dup 10) get-second) ; evaluates to 10
111 15. Define a "sixteen" function that makes
112 sixteen copies of its argument (and stores them in a data structure of
113 your choice).
114
115 16. Inspired by our definition of ordered pairs, propose a data structure capable of representin\
116 g ordered tripes. That is,
117         (((make-triple M) N) P)
118 should return an object that behaves in a reasonable way to serve as a triple. In addition to de\
119 fining the make-triple function, you have to show how to extraxt elements of your triple. Write \
120 a get-first-of-triple function, that does for triples what get-first does for pairs. Also write \
121 get-second-of-triple and get-third-of-triple functions.
122
123 > I expect some to come back with the lovely
124 >     (\f. f first second third)
125 > and others, schooled in a certain mathematical perversion, to come back
126 > with:
127 >     (\f. f first (\g. g second third))
128
129
130 17. Write a function second-plus-third that when given to your triple, returns the result of add\
131 ing the second and third members of the triple.
132
133 You can help yourself to the following definition:
134     (define add (lambda (x) (lambda (y) (+ x y))))
135