2c480096145d712a9f3018d9d8c289a43c3c9664
[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
18 determine whether those lists are equal: that is, whether they have all the
19 same members in the same order. (Equality for the lists we're working with is
20 *extensional*, or parasitic on the equality of their members, and the list
21 structure. Later in the course we'll see lists which aren't extensional in this
22 way.)
23
24 How would you implement such a list comparison?
25
26 (See [[hints/Assignment 4 hint 2]] if you need some hints.)
27 </OL>
28
29
30 #Enumerating the fringe of a leaf-labeled tree#
31
32 First, read this: [[Implementing trees]]
33
34 <OL start=3>
35 <LI>Write an implementation of leaf-labeled trees. You can do something v3-like, or use the Y combinator, as you prefer.
36
37 You'll need an operation `make_leaf` that turns a label into a new leaf. You'll
38 need an operation `make_node` that takes two subtrees (perhaps leaves, perhaps
39 other nodes) and joins them into a new tree. You'll need an operation `isleaf`
40 that tells you whether a given tree is a leaf. And an operation `extract_label`
41 that tells you what value is associated with a given leaf. And an operation
42 `extract_left` that tells you what the left subtree is of a tree that isn't a
43 leaf. (Presumably, `extract_right` will work similarly.)
44
45 <LI>The **fringe** of a leaf-labeled tree is the list of values at its leaves,
46 ordered from left to right. For example, the fringe of this tree:
47
48                 .
49            / \
50           .   3
51          / \
52         1   2
53
54 is `[1;2;3]`. And that is also the fringe of this tree:
55
56                 .
57            / \
58           1   .
59              / \
60         2   3
61
62 The two trees are different, but they have the same fringe. We're going to
63 return later in the term to the problem of determining when two trees have the
64 same fringe. For now, one straightforward way to determine this would be:
65 enumerate the fringe of the first tree. That gives you a list. Enumerate the
66 fringe of the second tree. That also gives you a list. Then compare the two
67 lists to see if they're equal. (You just programmed this above.)
68
69 Write the fringe-enumeration function. It should work on the implementation of
70 trees you designed in the previous step.
71 </OL>
72
73
74
75 #Mutually-recursive functions#
76
77 <OL start=5>
78 <LI>(Challenging.) One way to define the function `even` is to have it hand off
79 part of the work to another function `odd`:
80
81         let even = \x. iszero x
82                                         ; if x == 0 then result is
83                                         true
84                                         ; else result turns on whether x's pred is odd
85                                         (odd (pred x))
86
87 At the same tme, though, it's natural to define `odd` in such a way that it
88 hands off part of the work to `even`:
89
90         let odd = \x. iszero x
91                                         ; if x == 0 then result is
92                                         false
93                                         ; else result turns on whether x's pred is even
94                                         (even (pred x))
95
96 Such a definition of `even` and `odd` is called **mutually recursive**. If you
97 trace through the evaluation of some sample numerical arguments, you can see
98 that eventually we'll always reach a base step. So the recursion should be
99 perfectly well-grounded:
100
101         even 3
102         ~~> iszero 3 true (odd (pred 3))
103         ~~> odd 2
104         ~~> iszero 2 false (even (pred 2))
105         ~~> even 1
106         ~~> iszero 1 true (odd (pred 1))
107         ~~> odd 0
108         ~~> iszero 0 false (even (pred 0))
109         ~~> false
110
111 But we don't yet know how to implement this kind of recursion in the lambda
112 calculus.
113
114 The fixed point operators we've been working with so far worked like this:
115
116         let X = Y T in
117         X <~~> T X
118
119 Suppose we had a pair of fixed point operators, `Y1` and `Y2`, that operated on
120 a *pair* of functions `T1` and `T2`, as follows:
121
122         let X1 = Y1 T1 T2 in
123         let X2 = Y2 T1 T2 in
124         X1 <~~> T1 X1 X2 and
125         X2 <~~> T2 X1 X2
126
127 If we gave you such a `Y1` and `Y2`, how would you implement the above
128 definitions of `even` and `odd`?
129
130                                         
131 <LI>(More challenging.) Using our derivation of Y from the [Week3
132 notes](/week3/#index4h2) as a model, construct a pair `Y1` and `Y2` that behave
133 in the way described.
134
135 (See [[hints/Assignment 4 hint 4]] if you need some hints.)
136
137 </OL>
138