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

There's more...

The functions suml and sumr produce similar results because the combining function (+) is commutative, that is, a + b = b + a. However, when applied to map and filterthe results are different for mapr and mapl. This is because the list concatenation operator used for mapr and mapl is not commutative. Hence, the application of concatenation (:) to foldr and foldl produces different results. In fact, foldl would produce a reversed list. It is shown here (with elements [1..3]):

    -- Note both foldr and foldl are passed a function that adds an    element to the list.    foldr (:) [] [1..3] = 1 : foldr [] [2..3]                    = 1 : 2 : foldr [] [3]                    = 1 : 2 : 3 : []                    = [1,2,3] -- Produces same list foldl (\result x -> x : result) [1..3] = foldl (1 ...

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