[Haskell-beginners] count function

Daniel Fischer daniel.is.fischer at web.de
Sun Oct 18 12:51:41 EDT 2009


Am Sonntag 18 Oktober 2009 18:40:48 schrieb Robert Ziemba:
> On Sun, Oct 18, 2009 at 8:01 AM, Alexander Dunlap <
>
> alexander.dunlap at gmail.com> wrote:
> > The way I would count all of the lines in a file is (untested)
> >
> > fCountLines :: String -> IO Int
> > fCountLines = length . lines . readFile
>
> I'm not sure if I did something wrong, but I could not get this to work
> because 'readFile' returns an IO String.  I tried the following and it
> worked.
>
>  fileLines str = readFile str >>= return . lines >>= return . length
>
> Is this a reasonable way to count lines?

Almost. It should be

fileLines str = readFile str >>= return . length . lines

(or

return . length . lines =<< readFile str

or

fmap (length . lines) (readFile str)

or...)


More information about the Beginners mailing list