September 2017
Beginner to intermediate
396 pages
9h 46m
English
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 filter, the 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 ...
Read now
Unlock full access