[Shootout-list] sum a column of integers (ghc)

Greg Buchholz shootout@sleepingsquirrel.org
Fri, 1 Oct 2004 15:08:52 -0700


    Here's a version of the "Sum a column of integers" test for ghc
that's about 15 times faster than the current version.


-- compile with ghc -O2

import Char( ord )

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

fast_read ('-':xs) = -1 * fast_read(xs)
fast_read xs = foldr mult_acc 0 (reverse xs)

mult_acc = \x y -> ord(x)-ord('0') + 10 * y