4 Find "normal forms" for the following---that is, reduce them until no more reductions are possible. We'll write λ`x` as `\x`.
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))`
18 Recall our definitions of true and false.
20 > `true` defined to be `\t \f. t`
21 > `false` defined to be `\t \f. f`
23 In Racket, these can be defined like this:
25 (define true (lambda (t) (lambda (f) t)))
26 (define false (lambda (t) (lambda (f) f)))
29 <LI>Define a `neg` operator that negates `true` and `false`.
41 <LI>Define an `and` operator.
43 <LI>Define an `xor` operator.
45 (If you haven't seen this term before, here's a truth table:
50 false xor false = false
54 <LI>Inspired by our definition of boolean values, propose a data structure
55 capable of representing one of the two values `black` or `white`.
57 one of those values, call it a "black-or-white value", we should be able to
60 the-value if-black if-white
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
67 <LI>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.)
77 Recall our definitions of ordered pairs.
79 > the pair (x,y) is defined as `\f. f x y`
81 To extract the first element of a pair p, you write:
85 Here are some definitions in Racket:
87 (define make-pair (lambda (fst) (lambda (snd) (lambda (f) ((f fst) snd)))))
88 (define get-first (lambda (fst) (lambda (snd) fst)))
89 (define get-second (lambda (fst) (lambda (snd) snd)))
93 (define p ((make-pair 10) 20))
94 (p get-first) ; will evaluate to 10
95 (p get-second) ; will evaluate to 20
97 If you're bothered by having the pair to the left and the function that
98 operates on it come second, think about why it's being done this way: the pair
99 is a package that takes a function for operating on its elements as an
100 argument, and returns the result of operating on its elemens with that
101 function. In other words, the pair is also a function. (Of course, in the
102 untyped lambda calculus, absolutely *everything* is a function: functors,
103 arguments, abstracts, redexes, values---everything.)
105 If you like, you can disguise what's going on like this:
107 (define lifted-get-first (lambda (p) (p get-first)))
108 (define lifted-get-second (lambda (p) (p get-second)))
118 However, the latter is still what's going on under the hood.
122 <LI>Define a `swap` function that reverses the elements of a pair.
126 (define p ((make-pair 10) 20))
127 ((p swap) get-first) ; evaluates to 20
128 ((p swap) get-second) ; evaluates to 10
130 Write out the definition of swap in Racket.
133 <LI>Define a `dup` function that duplicates its argument to form a pair
134 whose elements are the same.
137 ((dup 10) get-first) ; evaluates to 10
138 ((dup 10) get-second) ; evaluates to 10
140 <LI>Define a `sixteen` function that makes
141 sixteen copies of its argument (and stores them in a data structure of
144 <LI>Inspired by our definition of ordered pairs, propose a data structure capable of representing ordered triples. That is,
146 (((make-triple M) N) P)
148 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.
150 <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.
152 You can help yourself to the following definition:
154 (define add (lambda (x) (lambda (y) (+ x y))))
156 <!-- 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.] -->