[Haskell-cafe] Software Tools in Haskell
Neil Mitchell
ndmitchell at gmail.com
Wed Dec 12 14:12:37 EST 2007
Hi
Having got to the word counting example on the website:
wordcount :: IO ()
wordcount = do
wc <- wordcount' False 0
putStrLn (show wc)
where
wordcount' inword wc = do
ch <- getc
case ch of
Nothing -> return wc
Just c -> handlechar c wc inword
handlechar c wc _ | (c == ' ' ||
c == '\n' ||
c == '\t') = wordcount' False wc
handlechar _ wc False = wordcount' True $! wc + 1
handlechar _ wc True = wordcount' True wc
Eeek. That's uglier than the C version, and has no abstract components.
A much simpler version:
main = print . length . words =<< getContents
Beautiful, specification orientated, composed of abstract components.
Code doesn't get much more elegant than that. Plus it also can be made
to outperform C (http://www-users.cs.york.ac.uk/~ndm/supero/)
Thanks
Neil
More information about the Haskell-Cafe
mailing list