edits
[lambda.git] / assignment3.mdwn
1 Assignment 3
2 ------------
3
4 Once again, the lambda evaluator will make working through this
5 assignment much faster and more secure.
6
7 *Writing recursive functions on version 1 style lists*
8
9 Recall that version 1 style lists are constructed like this:
10
11 <pre>
12 ; booleans
13 let true = \x y. x in
14 let false = \x y. y in
15 let and = \l r. l (r true false) false in
16
17 ; version 1 lists
18 let makePair = \f s g. g f s in
19 let fst = true in
20 let snd = false in
21 let nil = makePair true meh in
22 let isNil = \x. x fst in
23 let makeList = \h t. makePair false (makePair h t) in
24 let head = \l. isNil l err (l snd fst) in
25 let tail = \l. isNil l err (l snd snd) in
26
27 ; a list of numbers to experiment on
28 let mylist = makeList 1 (makeList 2 (makeList 3 nil)) in
29
30 ; a fixed-point combinator for defining recursive functions 
31 let Y = \f. (\h. f (h h)) (\h. f (h h)) in
32
33 ; church numerals
34 let isZero = \n. n (\x. false) true in
35 let succ = \n s z. s (n s z) in
36 let mult = \m n s. m (n s) in
37 let length = Y (\length l. isNil l 0 (succ (length (tail l)))) in
38 let predecessor = \n. length (tail (n (\p. makeList meh p) nil)) in
39 let leq = ; (leq m n) will be true iff m is less than or equal to n
40   Y (\leq m n. isZero m true (isZero n false (leq (predecessor m)(predecessor n)))) in
41 let eq = \m n. and (leq m n)(leq n m) in
42
43 eq 3 3
44 </pre>
45
46
47 Then `length mylist` evaluates to 3.
48
49 1. What does `head (tail (tail mylist))` evaluate to?
50
51 2. Using the `length` function as a model, and using the predecessor
52 function, write a function that computes factorials.  (Recall that n!,
53 the factorial of n, is n times the factorial of n-1.)
54
55 Warning: my browser isn't able to compute factorials of numbers
56 greater than 2 (it does't provide enough resources for the JavaScript
57 interpreter; web pages are not supposed to be that computationally
58 intensive).
59
60
61 3. Write a function `listLenEq` that returns true just in case two lists have the
62 same length.  That is,
63
64      listLenEq mylist (makeList meh (makeList meh (makeList meh nil))) ~~> true
65
66      listLenEq mylist (makeList meh (makeList meh nil))) ~~> false
67
68
69 4. Now write the same function, but don't use the length function (hint: use `leq` as a model).
70
71
72 [The following should be correct, but won't run in my browser:
73
74 let factorial = Y (\fac n. isZero n 1 (mult n (fac (predecessor n)))) in
75
76 <pre>
77 let reverse = 
78   Y (\rev l. isNil l nil 
79                    (isNil (tail l) l 
80                           (makeList (head (rev (tail l))) 
81                                     (rev (makeList (head l) 
82                                                    (rev (tail (rev (tail l))))))))) in
83
84 reverse (makeList 1 (makeList 2 (makeList 3 nil)))
85 </pre>
86
87 It may require more resources than my browser is willing to devote to
88 JavaScript.]
89