280ebf7f90887eb386cf7ba67bb78031c5a79846
[lambda.git] / exercises / assignment1_answers.mdwn
1 1.  Define a function `zero?` that expects a single number as an argument, and returns `'true` if that number is `0`, else returns `'false`.
2
3         let
4           zero? match lambda x. case x of
5                                   0 then 'true;
6                                   y then 'false
7                                 end
8         in zero?
9
10
11 2.  Define a function `empty?` that expects a sequence of values as an argument (doesn't matter what type of values), and returns `'true` if that sequence is the empty sequence `[]`, else returns `'false`.
12
13         let
14           empty? match lambda xs. case xs of
15                                     []    then 'true;
16                                     _ & _ then 'false
17                                   end
18         in empty?
19
20 3.  Define a function `tail` that expects a sequence of values as an argument (doesn't matter what type of values), and returns that sequence with the first element (if any) stripped away. (Applying `tail` to the empty sequence `[]` can just give us back the empty sequence.)
21
22         let
23           tail match lambda xs. case xs of
24                                   []      then [];
25                                   _ & xs' then xs'
26                                 end
27         in tail
28
29
30 4.  Define a function `drop` that expects two arguments, in the form (*number*, *sequence*), and works like this:
31
32         drop (0, [10, 20, 30])  # evaluates to [10, 20, 30]
33         drop (1, [10, 20, 30])  # evaluates to [20, 30]
34         drop (2, [10, 20, 30])  # evaluates to [30]
35         drop (3, [10, 20, 30])  # evaluates to []
36         drop (4, [10, 20, 30])  # evaluates to []
37
38     <!-- -->
39
40         letrec
41           drop match lambda (n, xs). case (n, xs) of
42                                        (0, _)       then xs;
43                                        (_, [])      then [];
44                                        (_, _ & xs') then drop (n-1, xs')
45                                      end
46         in drop
47
48     What is the relation between `tail` and `drop`?
49
50         let
51           tail xs = drop (1, xs)
52         in ...
53
54 5.  Define a function `take` that expects two arguments, in the same form as `drop`, but works like this instead:
55
56         take (0, [10, 20, 30])  # evaluates to []
57         take (1, [10, 20, 30])  # evaluates to [10]
58         take (2, [10, 20, 30])  # evaluates to [10, 20]
59         take (3, [10, 20, 30])  # evaluates to [10, 20, 30]
60         take (4, [10, 20, 30])  # evaluates to [10, 20, 30]
61
62     <!-- -->
63
64         letrec
65           take match lambda (n, xs). case (n, xs) of
66                                        (0, _)        then [];
67                                        (_, [])       then [];
68                                        (_, x' & xs') then x' & take (n-1, xs')
69                                      end
70         in take
71
72
73 6.  Define a function `split` that expects two arguments, in the same form as `drop` and `take`, but this time evaluates to a pair of results. It works like this:
74
75         split (0, [10, 20, 30])  # evaluates to ([], [10, 20, 30])
76         split (1, [10, 20, 30])  # evaluates to ([10], [20, 30])
77         split (2, [10, 20, 30])  # evaluates to ([10, 20], [30])
78         split (3, [10, 20, 30])  # evaluates to ([10, 20, 30], [])
79         split (4, [10, 20, 30])  # evaluates to ([10, 20, 30], [])
80
81     <!-- -->
82
83         letrec
84           split match lambda (n, xs). case (n, xs) of
85                                        (0, _)        then ([], xs);
86                                        (_, [])       then ([], []);
87                                        (_, x' & xs') then let
88                                                             (ys, zs) match split (n-1, xs')
89                                                           in (x' & ys, zs)
90                                      end
91         in split
92
93 7.  Write a function `filter` that expects two arguments. The second argument will be a sequence `xs` with elements of some type *t*, for example numbers. The first argument will be a function `p` that itself expects arguments of type *t* and returns `'true` or `'false`. What `filter` should return is a sequence that contains exactly those members of `xs` for which `p` returned `'true`.
94
95         letrec
96           filter match lambda (p, xs). case xs of
97                                          [] then [];
98                                          x' & xs' when p x' then x' & filter (p, xs');
99                                          _  & xs'           then      filter (p, xs')
100                                        end
101         in filter
102
103     The above solution uses [[pattern guards|/topics/week1_kapulet_advanced#guards]].
104
105
106 8.  Write a function `partition` that expects two arguments, in the same form as `filter`, but this time evaluates to a pair of results. It works like this:
107
108         partition (odd?, [11, 12, 13, 14])  # evaluates to ([11, 13], [12, 14])
109         partition (odd?, [11])              # evaluates to ([11], [])
110         partition (odd?, [12, 14])          # evaluates to ([], [12, 14])
111
112     <!-- -->
113
114         letrec
115           partition match lambda (p, xs). case xs of
116                                             []       then ([], []);
117                                             x' & xs' then let
118                                                             (ys, zs) match partition (p, xs')
119                                                           in if p x' then (x' & ys, zs) else (ys, x' & zs)
120                                           end
121         in partition
122
123
124 9.  Write a function `double` that expects one argument which is a sequence of numbers, and returns a sequence of the same length with the corresponding elements each being twice the value of the original element.
125
126         letrec
127           double match lambda xs. case xs of
128                                     []       then [];
129                                     x' & xs' then (2*x') & double xs'
130                                   end
131         in double
132
133
134 10.  Write a function `map` that generalizes `double`. This function expects a pair of arguments, the second being a sequence `xs` with elements of some type *t*, for example numbers. The first argument will be a function `f` that itself expects arguments of type *t* and returns some type *t'* of result. What `map` should return is a sequence of the results, in the same order as the corresponding original elements. The result should be that we could say:
135
136         letrec
137           map match lambda (f, xs). case xs of
138                                       []       then [];
139                                       x' & xs' then (f x') & map (f, xs')
140                                     end;
141           double match lambda xs. map ((lambda x. 2*x), xs)
142         in ...
143
144 11. Write a function `map2` that generalizes `map`. This function expects a triple of arguments: the first being a function `f` as for `map`, and the second and third being two sequences. In this case `f` is a function that expects *two* arguments, one from the first of the sequences and the other from the corresponding position in the other sequence. The result should behave like this:
145
146         map2 ((lambda (x,y). 10*x + y), [1, 2, 3], [4, 5, 6])  # evaluates to [14, 25, 36]
147
148     <!-- -->
149
150         letrec
151           map2 match lambda (f, xs, ys). case (xs, ys) of
152                                       ([], _)              then [];
153                                       (_, [])              then [];
154                                       (x' & xs', y' & ys') then (f x' y') & map2 (f, xs', ys')
155                                     end
156         in map2
157
158
159 ###Extra credit problems###
160
161 *   In class I mentioned a function `&&` which occupied the position *between* its arguments, rather than coming before them (this is called an "infix" function). The way that it works is that `[1, 2, 3] && [4, 5]` evaluates to `[1, 2, 3, 4, 5]`. Define this function, making use of `letrec` and the simpler infix operation `&`.
162
163         letrec
164           xs && ys = case xs of
165                        []       then ys;
166                        x' & xs' then x' & (xs' && ys)
167                      end
168         in (&&)
169
170     This solution is using a variation of [[the shorthand explained here|topics/week1_kapulet_advanced#funct-declarations]]. We didn't expect you'd know how to deal with the special syntax of `&&`. You might have just defined this using a regular name, like `append`.
171
172 *   Write a function `unmap2` that is something like the inverse of `map2`. This function expects two arguments, the second being a sequence of elements of some type *t*. The first is a function `g` that expects a single argument of type *t* and returns a *pair* of results, rather than just one result. We want to collate these results, the first into one sequence, and the second into a different sequence. Then `unmap2` should return those two sequences. Thus if:
173
174         g z1  # evaluates to (x1, y1)
175         g z2  # evaluates to (x2, y2)
176         g z3  # evaluates to (x3, y3)
177
178     Then `unmap2 (g, [z1, z2, z3])` should evaluate to `([x1, x2, x3], [y1, y2, y3])`.
179
180         letrec
181           unmap2 match lambda (g, zs). case zs of
182                                          []       then ([], []);
183                                          z' & zs' then let
184                                                          (x, y)   match g z';
185                                                          (xs, ys) match unmap2 (g, zs')
186                                                        in (x & xs, y & ys)
187                                        end
188         in unmap2
189
190 *   Write a function `takewhile` that expects a `p` argument like `filter`, and also a sequence. The result should behave like this:
191
192         takewhile ((lambda x. x < 10), [1, 2, 20, 4, 40]) # evaluates to [1, 2]
193
194     Note that we stop "taking" once we reach `20`, even though there are still later elements in the sequence that are less than `10`.
195
196         letrec
197           takewhile (p, xs) = case xs of
198                                 []       then [];
199                                 x' & xs' then if p x' then x' & takewhile (p, xs')
200                                                       else []
201                               end
202         in takewhile
203
204 *   Write a function `dropwhile` that expects a `p` argument like `filter`, and also a sequence. The result should behave like this:
205
206         dropwhile ((lambda x. x < 10), [1, 2, 20, 4, 40]) # evaluates to [20, 4, 40]
207
208     Note that we stop "dropping" once we reach `20`, even though there are still later elements in the sequence that are less than `10`.
209
210         letrec
211           dropwhile (p, xs) = case xs of
212                                 x' & xs' when p x' then dropwhile (p, xs');
213                                 _ & _              then xs;
214                                 []                 then []
215                               end
216         in dropwhile
217
218     Unlike the previous solution, this one uses [[pattern guards|/topics/week1_kapulet_advanced#guards]], merely for variety. (In this solution the last two case clauses could also be replaced by the single clause `_ then xs`.)
219
220 *   Write a function `reverse` that returns the reverse of a sequence. Thus, `reverse [1, 2, 3, 4]` should evaluate to `[4, 3, 2, 1]`.
221
222         letrec
223           aux (ys, xs) = case xs of
224                            []       then ys;
225                            x' & xs' then aux (x' & ys, xs')
226                          end;
227           reverse xs = aux ([], xs)
228         in reverse
229