[Haskell-cafe] Software Tools in Haskell

Don Stewart dons at galois.com
Wed Dec 12 14:17:16 EST 2007


ndmitchell:
> 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.

My thoughts too when reading the initial post was that it was all very
low level imperative programming. Not of the Haskell flavour.

-- Don


More information about the Haskell-Cafe mailing list