Use fold-family functions and some auxiliary functions, such as the map
, all
and any
defined in class slides, and your own, to define the following functions. In other words, you cannot use direct recursion to define them.
a. Write a function maxl xs
using that generates an error "empty list"
if xs==[]
and otherwise returns the largest element of xs
maxl :: (Ord a) => [a] -> a
maxl xs = ...
>maxl [1,3,2,5,6,4]
6
>maxl []
Program error: empty list
b. Write a function member x ys
which returns True
if x
is an element of ys
, False
otherwise. (No direct recursion)
member :: (Eq a) => [a] -> a -> Bool
member xs s = ...
> member [1,2,3] 4
False
> member [1,2,3,4.0,5] 4
True
> member [’a’,’b’,’x’,’z’] ’b’
True