In this recipe, we will look at two of the most important high-order functions, called foldr and foldl. These functions carry out the following activities:
- Abstract iterative process over a collection such as a list
- Give a way to work with each of the elements within the collection
- Give a way to summarize elements and combine them with user-supplied values
Depending on the way elements are combined, the functions are called foldr (fold right) or foldl (fold left). Many higher order functions such as map or filter can be expressed in terms of foldr or foldl.
In this recipe, we will write sum and product functions to calculate the sum and product of numbers in the input list respectively. We will also use folds to implement ...