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