[Haskell-cafe] Unwrapping long lines in text files
nowgate at yahoo.com
Sat Aug 14 19:43:52 EDT 2010
Thanks for the caveats.
I originally tried to write some pure functions to break up the long strings into ones of acceptable length, to no avail. The solution I submitted was a frustrated attempt to solve the problem *somehow* so I'd have something that worked to refer to.
I can see the benefit of separating IO from pure and will approach similar problems from that standpoint in the future.
--- On Sat, 8/14/10, Ben Millwood <haskell at benmachine.co.uk> wrote:
From: Ben Millwood <haskell at benmachine.co.uk>
Subject: Re: [Haskell-cafe] Unwrapping long lines in text files
To: "michael rice" <nowgate at yahoo.com>
Cc: haskell-cafe at haskell.org
Date: Saturday, August 14, 2010, 7:07 PM
On Sat, Aug 14, 2010 at 2:38 AM, michael rice <nowgate at yahoo.com> wrote:
> The program below takes a text file and unwraps all lines to 72 columns, but I'm getting an end of file message at the top of my output.
> How do I lose the EOF?
While many other people have shown you why you need not necessarily
answer this question, I think it'd be helpful for you to hear the
Your message is being produced because you are trying to getLine when
there is no input left. This raises an exception, which, because it is
not handled by your program, prints a diagnostic message and exits.
Strangely, it prints this before the output of your program - this
isn't terribly important, but for the sake of completeness, it's
because of the different buffering characteristics of stdout and
stderr, which confusingly mean that even though your program produces
output and then produces an error, the error is printed immediately
while the output waits until the program is terminated to be produced.
I think. Something like that, anyway.
So, how do you avoid the exception? You can use System.IO.isEOF  to
check if there is input available on the standard input handle:
main = do
eof <- isEOF
when (not eof) realMain
-- when from Control.Monad, see also: unless
realMain = do realStuff
Or you can let getLine throw the exception, but catch it and deal with
it yourself, rather than letting it kill your program.
Catching it is a little less simple, but is considerably more flexible
and powerful. The exceptions situation in Haskell is somewhat
complicated by the fact that the mechanism used by haskell98 has been
improved upon, but the new extensible mechanism is incompatible with
the old so both have to hang around. Have a look at Control.Exception
 and System.IO.Error . In either case you have 'catch' as a sort
of basic operation, and more convenient things like 'try' which sort
of turn an exception into a pure Either result. I'd do something like
main = do
result <- try getLine
case result of
Left err -> return () -- or do something diagnostic
Right "" -> putStrLn "" >> main
Right line -> doStuffWith line
On a more general note, your main function looks a little suspect
because it *always* recurses into itself - there's no termination
condition. A question nearly as important as "how does my program know
what to do" is "how does it know when to stop" :)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe