[Shootout-list] Sum a column of Integers (ghc)
Greg Buchholz
sleepingsquirrel@member.fsf.org
Sat, 2 Oct 2004 18:52:08 -0700 (PDT)
--0-1694158503-1096768328=:55727
Content-Type: text/plain; charset=us-ascii
Content-Id:
Content-Disposition: inline
Here's a much faster version of the "Sum a Column of Integers"
test for ghc. It's not quite as concise as the original, but I
think it is still understandable and it is about 85 times faster.
(BTW, does the mailing list prefer file attachments or just inline
text for program code?)
Greg Buchholz
_______________________________
Do you Yahoo!?
Declare Yourself - Register online to vote today!
http://vote.yahoo.com
--0-1694158503-1096768328=:55727
Content-Type: text/x-haskell; name="sum_col.hs"
Content-Description: sum_col.hs
Content-Disposition: inline; filename="sum_col.hs"
-- Sum a Column of Integers for ghc
-- compile with : ghc -O2 -o sum_col sum_col.hs
-- To get better performance set default heap size to 10MB
-- i.e. invoke as : ./sum_col +RTS -H10M <input_file.txt
import Char( ord )
main = getContents >>= print . acc 0 False 0
acc run False init [] = run+init
acc run True init [] = run-init
acc run False init ('\n':xs) = acc (run+init) False 0 xs
acc run True init ('\n':xs) = acc (run-init) False 0 xs
acc run _ init ('-' :xs) = acc run True init xs
acc run sign init (x:xs) = acc run sign (init*10+ord(x)-ord('0')) xs
--0-1694158503-1096768328=:55727--