edits
[lambda.git] / assignment4.mdwn
1 #Reversing a list#
2
3 <OL>
4 <LI>How would you define an operation to reverse a list? (Don't peek at the
5 [[lambda_library]]! Try to figure it out on your own.) Choose whichever
6 implementation of list you like. Even then, there are various strategies you
7 can use.
8
9 (See [[hints/Assignment 4 hint 1]] if you need some hints.)
10 </OL>
11
12
13 #Comparing lists for equality#
14
15
16 <OL start=2>
17 <LI>Suppose you have two lists of integers, `left` and `right`. You want to determine whether those lists are equal: that is, whether they have all the same members in the same order. (Equality for the lists we're working with is *extensional*, or parasitic on the equality of their members, and the list structure. Later in the course we'll see lists which aren't extensional in this way.)
18
19 How would you implement such a list comparison?
20
21 (See [[hints/Assignment 4 hint 2]] if you need some hints.)
22 </OL>
23
24
25 #Enumerating the fringe of a leaf-labeled tree#
26
27 First, read this: [[Implementing trees]]
28
29 <OL start=3>
30 <LI>blah
31
32 (See [[hints/Assignment 4 hint 3]] if you need some hints.)
33 </OL>
34
35
36 #Mutually-recursive functions#
37
38 <OL start=4>
39 <LI>(Challenging.) One way to define the function `even` is to have it hand off part of the work to another function `odd`:
40
41         let even = \x. iszero x
42                                         ; if x == 0 then result is
43                                         true
44                                         ; else result turns on whether x's pred is odd
45                                         (odd (pred x))
46
47 At the same tme, though, it's natural to define `odd` in such a way that it hands off part of the work to `even`:
48
49         let odd = \x. iszero x
50                                         ; if x == 0 then result is
51                                         false
52                                         ; else result turns on whether x's pred is even
53                                         (even (pred x))
54
55 Such a definition of `even` and `odd` is called **mutually recursive**. If you trace through the evaluation of some sample numerical arguments, you can see that eventually we'll always reach a base step. So the recursion should be perfectly well-grounded:
56
57         even 3
58         ~~> iszero 3 true (odd (pred 3))
59         ~~> odd 2
60         ~~> iszero 2 false (even (pred 2))
61         ~~> even 1
62         ~~> iszero 1 true (odd (pred 1))
63         ~~> odd 0
64         ~~> iszero 0 false (even (pred 0))
65         ~~> false
66
67 But we don't yet know how to implement this kind of recursion in the lambda calculus.
68
69 The fixed point operators we've been working with so far worked like this:
70
71         let X = Y T in
72         X <~~> T X
73
74 Suppose we had a pair of fixed point operators, `Y1` and `Y2`, that operated on a *pair* of functions `T1` and `T2`, as follows:
75
76         let X1 = Y1 T1 T2 in
77         let X2 = Y2 T1 T2 in
78         X1 <~~> T1 X1 X2 and
79         X2 <~~> T2 X1 X2
80
81 If we gave you such a `Y1` and `Y2`, how would you implement the above definitions of `even` and `odd`?
82
83                                         
84 <LI>(More challenging.) Using our derivation of Y from the [[Week2]] notes as a model, construct a pair `Y1` and `Y2` that behave in the way described.
85
86 (See [[hints/Assignment 4 hint 4]] if you need some hints.)
87
88 </OL>
89