high order functions Flashcards
what is a high order function
a function that takes a function as a parameter, returns a function or both
what are benefits of high order functions
allows flexible and reusable functions
allows you to encode common programming patterns(encapsulation)
enables function composition
function composition
combining functions for complex behaviours
what are some examples of high order functions
map
filter
foldr
all
any
(.)
takeWhile
dropWhile
map
applies a function to all values in a list
filter
selects every element that satisfies the function
all
returns all elements that satisfy the function
any
returns the first element that satisfies the function
(.)
returns the composition of two functions
takeWhile
takes elements from the start of the list that satsify the function/ predicate
stops when it gets to the first that doesnt
dropWhile
drops elements from the start of the list that satsify the function/ predicate
stops when it gets to the first that doesnt
foldr
recursive pattern
f[] = v
f(x:xs) = x # f xs
#: function
local bindings
variables or functions that are defined within a limited scope
usually inside a function
what can we use to do local bindings
where
let in
let in
let: defines the local bindings
in: the bindings are only available in this expression
where
introduces local bindings available for the entire function body
foldr
recursively applies the given function to the list from right to left
foldl
recursively applies the given function to the list from left to right
list comprehension
constructs new lists from old lists
what does list comprehension look like in haskell
[x^2 | x <- [1..5]]
which part is the generator in list comprehension and why
x <- [1..5]]
states how to generate values for x
dependant generator
when later generators depends on the variables introduced in the earlier generators
what is the role of guards in list comprehension
the condition that limits the values that the generator can use
zip
maps two lists to a list of pairs of their corresponding elements