560d288ebc4e7687af786a57f1eccb4a67b05fca
[lambda.git] / assignment1.mdwn
1 Reduction
2 ---------
3
4 Find "normal forms" for the following (that is, reduce them as far as it's possible to reduce 
5 them):
6
7     1. (\x \y. y x) z
8     2. (\x (x x)) z
9     3. (\x (\x x)) z
10     4. (\x (\z x)) z
11     5. (\x (x (\y y))) (\z (z z))
12     6. (\x (x x)) (\x (x x))
13     7. (\x (x x x)) (\x (x x x))
14
15
16 Booleans
17 --------
18
19 Recall our definitions of true and false.
20
21         "true" defined to be `\t \f. t`
22         "false" defined to be `\t \f. f`
23
24 In Racket, these can be defined like this:
25
26         (define true (lambda (t) (lambda (f) t)))
27         (define false (lambda (t) (lambda (f) f)))
28
29 * Define a "neg" operator that negates "true" and "false".
30
31 Expected behavior: 
32
33     (((neg true) 10) 20)
34
35 evaluates to 20, and 
36
37     (((neg false) 10) 20)
38
39 evaluates to 10.
40
41 * Define an "and" operator.
42
43 * Define an "xor" operator. 
44
45 (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 )
53
54 * Inspired by our definition of boolean values, propose a data structure
55 capable of representing one of the two values "black" or "white". 
56 If we have
57 one of those values, call it a black-or-white-value, we should be able to
58 write:
59
60         the-black-or-white-value if-black if-white
61
62 (where if-black and if-white are anything), and get back one of if-black or
63 if-white, depending on which of the black-or-white values we started with. Give
64 a definition for each of "black" and "white". (Do it in both lambda calculus
65 and also in Racket.)
66
67 * Now propose a data structure capable of representing one of the three values
68 "red" "green" or "blue," based on the same model. (Do it in both lambda
69 calculus and also in Racket.)
70
71
72
73 Pairs
74 -----
75
76 Recall our definitions of ordered pairs.
77
78         the pair (x,y) is defined as `\f. f x y`
79
80 To extract the first element of a pair p, you write:
81
82         p (\fst \snd. fst)
83
84 Here are some defintions in Racket:
85
86         (define make-pair (lambda (fst) (lambda (snd) (lambda (f) ((f fst) snd)))))
87         (define get-first (lambda (fst) (lambda (snd) fst)))
88         (define get-second (lambda (fst) (lambda (snd) snd)))
89
90 Now we can write:
91
92         (define p ((make-pair 10) 20))
93         (p get-first)   ; will evaluate to 10
94         (p get-second)  ; will evaluate to 20
95
96 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.
97
98 If you like, you can disguise what's going on like this:
99
100         (define lifted-get-first (lambda (p) (p get-first)))
101         (define lifted-get-second (lambda (p) (p get-second)))
102
103 Now you can write:
104
105         (lifted-get-first p)
106
107 instead of:
108
109         (p get-first)
110
111 However, the latter is still what's going on under the hood.
112
113
114 13. Define a "swap" function that reverses the elements of a pair.
115 Expected behavior:
116
117         (define p ((make-pair 10) 20))
118         ((p swap) get-first) ; evaluates to 20
119         ((p swap) get-second) ; evaluates to 10
120
121 Write out the definition of swap in Racket.
122
123
124 14. Define a "dup" function that duplicates its argument to form a pair
125 whose elements are the same.
126 Expected behavior:
127
128         ((dup 10) get-first) ; evaluates to 10
129         ((dup 10) get-second) ; evaluates to 10
130
131 15. Define a "sixteen" function that makes
132 sixteen copies of its argument (and stores them in a data structure of
133 your choice).
134
135 16. Inspired by our definition of ordered pairs, propose a data structure capable of representing ordered tripes. That is,
136
137         (((make-triple M) N) P)
138
139 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.
140
141 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.
142
143 You can help yourself to the following definition:
144
145     (define add (lambda (x) (lambda (y) (+ x y))))
146
147 18. [Super hard, unless you have lots of experience programming] Write a function that reverses the order of the elements in a list.