[Shootout-list] Better sumcol for Haskell

John Goerzen jgoerzen@complete.org
Tue, 28 Sep 2004 21:13:51 +0000 (UTC)


Here's a better sumcol for Haskell:

main = interact ((++ "\n") . show . foldl (flip $ (+) . read) 0 . lines)

Or this equivolent version:

main = interact ((++ "\n") . show . foldl (\c -> (+ c) . read) 0 .  lines)

Or this more verbose one yet:

main = interact ((++ "\n") . show . foldl (\c l -> c + (read l)) 0 .  lines)

It runs 3 times faster on my machine and will require far less
memory since it's composed entirely of tail-recursive functions (you can
also avoid adding command-line arguments to give it more stack space).

It also fixes a bug.  The current version prints the \n before the
result, not after.  Somewhat silly to do that :-)

-- 
John Goerzen
Author, Foundations of Python Network Programming
http://www.amazon.com/exec/obidos/tg/detail/-/1590593715