[Haskell-beginners] How to unnest "do"

Martin Drautzburg Martin.Drautzburg at web.de
Sun Jan 27 18:27:48 CET 2013


Hello all,

in the code snippet below, is there a way to factor out the second "do"?

import System (getArgs) 
main :: IO () 
main = do 
        args <- getArgs 
        case args of 
                [fname] ->  do fstr <- readFile fname 
                               let nWords = length . words $ fstr 
                                   nLines = length . lines $ fstr 
                                   nChars = length fstr 
                               putStrLn . unwords $ [ show nLines 
                                             , show nWords 
                                             , show nChars 
                                             , fname] 
                _ ->putStrLn "usage: wc fname"






More information about the Beginners mailing list