Answers
- You can make a function more general by replacing one of the (other) functions it calls with a parameter. The type of this function parameter will be the same as the type of the function abstracted over. Further generality is often possible at this point by generalizing the types by means of parametric polymorphism.
- The
foldr
function captures the structural recursion scheme for lists:foldr :: (a -> b -> b) -> b -> [a] -> b foldr c n [] = n foldr c n (x:xs) = c x (foldr c n xs)
The
n
parameter abstracts over the result for the base constructor,[]
, and thec
function parameter for the recursive constructor,(:)
. Similar fold functions can be written for other algebraic datatypes. - Notable predefined HOF are as follows:
map :: (a -> b) -> [a] -> [
b]
filter :: (a -> Bool) -> [a] -> [
a]
any :: (a -> Bool) -> [a] ->
Bool
all :: (a -> Bool) -> [a] ->
Bool
takeWhile :: (a -> Bool) -> [a] -...