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`.
4 zero? match lambda x. case x of
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`.
14 empty? match lambda xs. case xs of
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.)
23 tail match lambda xs. case xs of
30 4. Define a function `drop` that expects two arguments, in the form (*number*, *sequence*), and works like this:
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 []
41 drop match lambda (n, xs). case (n, xs) of
44 (_, _ & xs') then drop (n-1, xs')
48 What is the relation between `tail` and `drop`?
51 tail xs = drop (1, xs)
54 5. Define a function `take` that expects two arguments, in the same form as `drop`, but works like this instead:
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]
65 take match lambda (n, xs). case (n, xs) of
68 (_, x' & xs') then x' & take (n-1, xs')
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:
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], [])
84 split match lambda (n, xs). case (n, xs) of
86 (_, []) then ([], []);
87 (_, x' & xs') then let
88 (ys, zs) match split (n-1, xs')
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`.
96 filter match lambda (p, xs). case xs of
98 x' & xs' when p x' then x' & filter (p, xs');
99 _ & xs' then filter (p, xs')
103 The above solution uses [[pattern guards|/topics/week1_kapulet_advanced#guards]].
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:
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])
115 partition match lambda (p, xs). case xs of
118 (ys, zs) match partition (p, xs')
119 in if p x' then (x' & ys, zs) else (ys, x' & zs)
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.
127 double match lambda xs. case xs of
129 x' & xs' then (2*x') & double xs'
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:
137 map match lambda (f, xs). case xs of
139 x' & xs' then (f x') & map (f, xs')
141 double match lambda xs. map ((lambda x. 2*x), xs)
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:
146 map2 ((lambda (x,y). 10*x + y), [1, 2, 3], [4, 5, 6]) # evaluates to [14, 25, 36]
151 map2 match lambda (f, xs, ys). case (xs, ys) of
154 (x' & xs', y' & ys') then (f x' y') & map2 (f, xs', ys')
159 ###Extra credit problems###
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 `&`.
164 xs && ys = case xs of
166 x' & xs' then x' & (xs' && ys)
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`.
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:
174 g z1 # evaluates to (x1, y1)
175 g z2 # evaluates to (x2, y2)
176 g z3 # evaluates to (x3, y3)
178 Then `unmap2 (g, [z1, z2, z3])` should evaluate to `([x1, x2, x3], [y1, y2, y3])`.
181 unmap2 match lambda (g, zs). case zs of
185 (xs, ys) match unmap2 (g, zs')
190 * Write a function `takewhile` that expects a `p` argument like `filter`, and also a sequence. The result should behave like this:
192 takewhile ((lambda x. x < 10), [1, 2, 20, 4, 40]) # evaluates to [1, 2]
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`.
197 takewhile (p, xs) = case xs of
199 x' & xs' then if p x' then x' & takewhile (p, xs')
204 * Write a function `dropwhile` that expects a `p` argument like `filter`, and also a sequence. The result should behave like this:
206 dropwhile ((lambda x. x < 10), [1, 2, 20, 4, 40]) # evaluates to [20, 4, 40]
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`.
211 dropwhile (p, xs) = case xs of
212 x' & xs' when p x' then dropwhile (p, xs');
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`.)
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]`.
223 aux (ys, xs) = case xs of
225 x' & xs' then aux (x' & ys, xs')
227 reverse xs = aux ([], xs)