-- -- The Computer Language Shootout -- http://shootout.alioth.debian.org/ -- -- Contributed by Don Stewart -- Based on older versions by Greg Buchholz, -- Mirko Rahn, Chris Kuklewicz and David Himmelstrup -- import Data.Char #ifndef __NHC__ import Data.ByteString.Base import qualified Data.ByteString.Char8 as B #else import Prelude as B #endif main = print . new 0 =<< B.getContents new i s | B.null s = i | x == '-' = sub i 0 xs | otherwise = add i (parse x) xs where (x,xs) = uncons s sub i n t | i `seq` n `seq` t `seq` False = undefined sub i n t | y == '\n' = new (i-n) ys | otherwise = sub i n' ys where (y,ys) = uncons t n' = parse y + 10 * n add i n t | i `seq` n `seq` t `seq` False = undefined add i n t | y == '\n' = new (i+n) ys | otherwise = add i n' ys where (y,ys) = uncons t n' = parse y + 10 * n parse c = ord c - ord '0' #ifndef __NHC__ uncons s = (w2c (unsafeHead s), unsafeTail s) #else uncons (x:xs) = (x,xs) #endif