7. Higher-order functions Flashcards
Function for applying a function to every item in a list
map :: (a -> b) -> [a] -> [b]
map f [ ] = [ ]
map f (x:xs) = f x : map f xs
Function for filtering elements of a list with a specific property
filter :: (a -> Bool) -> [a] -> [a]
filter p [ ] = [ ]
filter p (x:xs) | p x = x : filter p xs
| otherwise = filter p xs
What is the syntax of foldr (recursive)
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr f v [ ] = v
foldr f v (x:xs) = f x (foldr f v xs)
what is the order of execution given:
foldr (#) v [x0, x1, .. , xn] =
x0 # (x1 # (… (xn # v) … ))
what is the syntax of foldl (recursive)
foldl :: (a -> b -> a) -> a -> [b] -> a
fold f v [ ] = v
foldl f v (x:xs) = foldl f (f v x) xs
what is the order of execution given:
foldl (#) v [x0, x1, … , xn]
(… ((v # x0) # x1 …) # xn
describe the composition function
(.) :: (b -> c) -> (a -> b) -> (a -> c)
f . g = \x -> f (g x)