O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with folds

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  ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required