-- $Id: wc.ghc,v 1.2 2001/05/24 14:05:53 doug Exp $ -- http://www.bagley.org/~doug/shootout/ -- from Brian Gregor module Main where -- compile with: ghc -O -o wc -package lang wc.hs import IO import IOExts import PackedString main = do -- set buffer to 4k hSetBuffering stdin (BlockBuffering (Just 4096)) -- these are mutable variables nL <- newIORef 0 nW <- newIORef 0 nC <- newIORef 0 (nl,nw,nc) <- countAll nL nW nC putStrLn ((show nl)++" "++(show nw)++" "++(show nc)) countAll :: IORef Int -> IORef Int -> IORef Int -> IO (Int,Int,Int) countAll nL nW nC = do end <- hIsEOF stdin nl <- readIORef nL nw <- readIORef nW nc <- readIORef nC if (not end) then (do inStr <- hGetLine stdin -- using a packed string is a small speed win let str = packString inStr -- using IORefs makes it easy to force strict -- evaluation - how to easily do this without -- IORefs? writeIORef nL $! (nl + 1) writeIORef nW $! (nw + (length (wordsPS str))) writeIORef nC $! (nc + 1 + (lengthPS str)) countAll nL nW nC) else return (nl,nw,nc)