Basic functions Flashcards
1
Q
Explain:
fold functions (reduce)
A
This is family of higher-order functions which are used to transform/fold/reduce a data structure to a value by application of a provided function to the elements of the structure.
There are various approaches to folding but the most interesting are
-
foldl - left fold ((((0 + 1) + 2) + 3) + 4)
- fails for infinite lists, builds up memory leaks
-
foldr - right fold (0+ (1 + (2 + (3 +4))))
- can work with infinite lists with special care.
Intuition with folds is that they are replacing the constructor in a data structure to produce the effect of reduction of the structure.
Lot of other function can be implemented as folds: inverse, map…