9be95780ca0faf912b436a7fe13e81f0d6c0cf8d
[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
78         (define p ((make-pair 10) 20))
79         (p get-first)   ; will evaluate to 10
80         (p get-second)  ; will evaluate to 20
81
82 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.
83
84 If you like, you can disguise what's going on like this:
85
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
91         (lifted-get-first p)
92
93 instead of:
94
95         (p get-first)
96
97 However, the latter is still what's going on under the hood.
98
99
100 13. Define a "swap" function that reverses the elements of a pair.
101 Expected behavior:
102
103         (define p ((make-pair 10) 20))
104         ((p swap) get-first) ; evaluates to 20
105         ((p swap) get-second) ; evaluates to 10
106
107 Write out the definition of swap in Racket.
108
109
110 14. Define a "dup" function that duplicates its argument to form a pair
111 whose elements are the same.
112 Expected behavior:
113
114         ((dup 10) get-first) ; evaluates to 10
115         ((dup 10) get-second) ; evaluates to 10
116
117 15. Define a "sixteen" function that makes
118 sixteen copies of its argument (and stores them in a data structure of
119 your choice).
120
121 16. Inspired by our definition of ordered pairs, propose a data structure capable of representing ordered tripes. That is,
122
123         (((make-triple M) N) P)
124
125 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.
126
127 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.
128
129 You can help yourself to the following definition:
130
131     (define add (lambda (x) (lambda (y) (+ x y))))
132
133 18. [Super hard, unless you have lots of experience programming] Write a function that reverses the order of the elements in a list.