+ The second `case` clause could also just be `_ then 'false`.
+
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.)
let
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.)
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`.
letrec
in split
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`.
letrec
- filter match lambda (p, xs). case xs of
- [] then [];
- x' & xs' when p x' then x' & filter (p, xs');
- _ & xs' then filter (p, xs')
- end
+ filter (p, xs) = case xs of
+ [] then [];
+ x' & xs' when p x' then x' & filter (p, xs');
+ _ & xs' then filter (p, xs')
+ end
in filter
The above solution uses [[pattern guards|/topics/week1_kapulet_advanced#guards]].
in filter
The above solution uses [[pattern guards|/topics/week1_kapulet_advanced#guards]].
- in if p x' then (x' & ys, zs) else (ys, x' & zs)
- end
+ partition (p, xs) = case xs of
+ [] then ([], []);
+ x' & xs' then let
+ (ys, zs) match partition (p, xs')
+ in if p x' then (x' & ys, zs) else (ys, x' & zs)
+ end
in partition
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.
letrec
in partition
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.
- 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`.)
+ 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`.)
* Write a function `reverse` that returns the reverse of a sequence. Thus, `reverse [1, 2, 3, 4]` should evaluate to `[4, 3, 2, 1]`.
* Write a function `reverse` that returns the reverse of a sequence. Thus, `reverse [1, 2, 3, 4]` should evaluate to `[4, 3, 2, 1]`.