[Haskell-cafe] Doubting Haskell
John Meacham
john at repetae.net
Sun Feb 17 20:09:20 EST 2008
On Sat, Feb 16, 2008 at 05:04:53PM -0800, Donn Cave wrote:
> But in Haskell, you cannot read a file line by line without writing an
> exception handler, because end of file is an exception! as if a file does
> not normally have an end where the authors of these library functions
> came from?
Part of it is that using 'getLine' is not idiomatic haskell when you
don't want to worry about exceptions. Generally you do something like
doMyThing xs = print (length xs)
main = do
contents <- readFile "my.file"
mapM_ doMyThing (lines contents)
which will call 'doMyThing' on each line of the file, in this case
printing the length of each line.
or more succinctly:
main = readFile "my.file" >>= mapM_ doMyThing . lines
John
--
John Meacham - ⑆repetae.net⑆john⑈
More information about the Haskell-Cafe
mailing list