(no commit message)
[lambda.git] / assignment1.mdwn
1 Reduction
2 ---------
3
4 Find "normal forms" for the following---that is, reduce them until no more reductions are possible. We'll write <code>&lambda;x</code> as `\x`.
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 can be defined like this:
24
25         (define true (lambda (t) (lambda (f) t)))
26         (define false (lambda (t) (lambda (f) f)))
27
28 <OL start=8>
29 <LI>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 <LI>Define an `and` operator.
42
43 <LI>Define an `xor` operator. If you haven't seen this term before, here's a truth table:
44
45     true xor true = false
46     true xor false = true
47     false xor true = true
48     false xor false = false
49
50
51 <LI>Inspired by our definition of boolean values, propose a data structure
52 capable of representing one of the two values `black` or `white`. 
53 If we have
54 one of those values, call it a "black-or-white value", we should be able to
55 write:
56
57         the-value if-black if-white
58
59 (where `if-black` and `if-white` are anything), and get back one of `if-black` or
60 `if-white`, depending on which of the black-or-white values we started with. Give
61 a definition for each of `black` and `white`. (Do it in both lambda calculus
62 and also in Racket.)
63
64 <LI>Now propose a data structure capable of representing one of the three values
65 `red` `green` or `blue`, based on the same model. (Do it in both lambda
66 calculus and also in Racket.)
67 </OL>
68
69
70
71 Pairs
72 -----
73
74 Recall our definitions of ordered pairs.
75
76 >   the pair **(**x**,**y**)** is defined to be `\f. f x y`
77
78 To extract the first element of a pair p, you write:
79
80         p (\fst \snd. fst)
81
82 Here are some definitions in Racket:
83
84         (define make-pair (lambda (fst) (lambda (snd) (lambda (f) ((f fst) snd)))))
85         (define get-first (lambda (fst) (lambda (snd) fst)))
86         (define get-second (lambda (fst) (lambda (snd) snd)))
87
88 Now we can write:
89
90         (define p ((make-pair 10) 20))
91         (p get-first)   ; will evaluate to 10
92         (p get-second)  ; will evaluate to 20
93
94 If you're puzzled by having the pair to the left and the function that
95 operates on it come second, think about why it's being done this way: the pair
96 is a package that takes a function for operating on its elements *as an
97 argument*, and returns *the result of* operating on its elements with that
98 function. In other words, the pair is a higher-order function. (Consider the similarities between this definition of a pair and a generalized quantifier.)
99
100 If you like, you can disguise what's going on like this:
101
102         (define lifted-get-first (lambda (p) (p get-first)))
103         (define lifted-get-second (lambda (p) (p get-second)))
104
105 Now you can write:
106
107         (lifted-get-first p)
108
109 instead of:
110
111         (p get-first)
112
113 However, the latter is still what's going on under the hood. (Remark: `(lifted-f ((make-pair 10) 20))` stands to `(((make-pair 10) 20) f)` as `(((make-pair 10) 20) f)` stands to `((f 10) 20)`.)
114
115
116 <OL start=13>
117 <LI>Define a `swap` function that reverses the elements of a pair. Expected behavior:
118
119         (define p ((make-pair 10) 20))
120         ((p swap) get-first) ; evaluates to 20
121         ((p swap) get-second) ; evaluates to 10
122
123 Write out the definition of `swap` in Racket.
124
125
126 <LI>Define a `dup` function that duplicates its argument to form a pair
127 whose elements are the same.
128 Expected behavior:
129
130         ((dup 10) get-first) ; evaluates to 10
131         ((dup 10) get-second) ; evaluates to 10
132
133 <LI>Define a `sixteen` function that makes
134 sixteen copies of its argument (and stores them in a data structure of
135 your choice).
136
137 <LI>Inspired by our definition of ordered pairs, propose a data structure capable of representing ordered triples. That is,
138
139         (((make-triple M) N) P)
140
141 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 extract 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.
142
143 <LI>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.
144
145 You can help yourself to the following definition:
146
147         (define add (lambda (x) (lambda (y) (+ x y))))
148
149 <!-- Write a function that reverses the order of the elements in a list. [Only attempt this problem if you're feeling frisky, it's super hard unless you have lots of experience programming.]  -->
150
151 </OL>
152