|
|
|
|
|
by contravariant
3791 days ago
|
|
One particular example that I recall was that if you calculate something like foldl (+) 0 [1..1000000]
(i.e. calculate the sum of the first 1000000 natural numbers)Then the obvious way to do it is to simply keep a running total, but haskell doesn't do that, it constructs the expression: (...((1+2)+3)+4)+5)+...)+1000000)
And tries to evaluate that, which tends to cause stack overflows, and is just generally slow. To fix it you have to use the following expression instead: foldl' (+) 0 [1..1000000]
This problem is described in more detail here: https://wiki.haskell.org/Foldr_Foldl_Foldl' |
|