Open akamayu-ouo opened 3 years ago
Correct. If the operations commutate, then the myFoldl
definition from the solution does work. Another noncommutative example would be
(foldr . flip) (mod) 4 [3, 2] -- outputs 0 whilst 1 would be correct
To mimic the correct behaviour, one has also to reverse the input list to have the function application in the correct order:
myFoldl :: (b -> a -> b) -> b -> [a] -> b
myFoldl f base xs = foldr (flip f) base (reverse xs)
Hi, I find a testcase that make the
myFoldl
function fail.What's wrong is after flipping the reduction function, the arguments are swapped, but the order of application is still from right to left.
More explicit,
while