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

How it works...

Foldr has a type declaration as shown here:

    foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b

Whereas, foldl has a type declaration as shown here:

    foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b

The part of the declaration Foldable t denotes that folds can be implemented by any data type that would declare itself to be Foldable (Foldable is a type class). In the context of list, the preceding function can be adapted as follows:

    foldr :: (a -> b -> b) -> b -> [a] -> b    foldl :: (b -> a -> b) -> b -> [a] -> b

Foldr takes three arguments, which are described here:

  • (a -> b -> b): This is the function that takes the element of type a of the list (or Foldable), another value of type b, and produces b
  • b: This is the ...

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