7. Higher-order functions Flashcards

1
Q

Function for applying a function to every item in a list

A

map :: (a -> b) -> [a] -> [b]
map f [ ] = [ ]
map f (x:xs) = f x : map f xs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Function for filtering elements of a list with a specific property

A

filter :: (a -> Bool) -> [a] -> [a]
filter p [ ] = [ ]
filter p (x:xs) | p x = x : filter p xs
| otherwise = filter p xs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the syntax of foldr (recursive)

A

foldr :: (a -> b -> b) -> b -> [a] -> b
foldr f v [ ] = v
foldr f v (x:xs) = f x (foldr f v xs)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is the order of execution given:
foldr (#) v [x0, x1, .. , xn] =

A

x0 # (x1 # (… (xn # v) … ))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is the syntax of foldl (recursive)

A

foldl :: (a -> b -> a) -> a -> [b] -> a
fold f v [ ] = v
foldl f v (x:xs) = foldl f (f v x) xs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is the order of execution given:
foldl (#) v [x0, x1, … , xn]

A

(… ((v # x0) # x1 …) # xn

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

describe the composition function

A

(.) :: (b -> c) -> (a -> b) -> (a -> c)
f . g = \x -> f (g x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly