[Haskell-cafe] Software Tools in Haskell

Conal Elliott conal at conal.net
Wed Dec 12 14:20:52 EST 2007


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 at 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/<http://www-users.cs.york.ac.uk/%7Endm/supero/>
> )
>
> Thanks
>
> Neil
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20071212/8c2f7825/attachment.htm


More information about the Haskell-Cafe mailing list