[Haskell] specification of sum

Scherrer, Chad Chad.Scherrer at pnl.gov
Tue Nov 1 15:19:03 EST 2005

I was wondering... In my experience, it's worked much better to use

sum' = foldl' (+) 0

than the built-in "sum" function, which leaks memory like crazy for
large input lists. I'm guessing the built-in definition is

sum = foldr (+) 0

But as far as I know, (+) is always strict, so foldl' seems much more
natural to me. Is there a case where the build-in definition is

Chad Scherrer
Computational Mathematics Group
Pacific Northwest National Laboratory

"Time flies like an arrow; fruit flies like a banana." -- Groucho Marx

More information about the Haskell mailing list