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