Here's a version with cleaner separation between pure & IO:

main = interact $ show . length . words

  - Conal

On Dec 12, 2007 11:12 AM, Neil Mitchell < ndmitchell@gmail.com> wrote:
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
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe