<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Thanks for the caveats.<br><br>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.<br><br>I can see the benefit of separating IO from pure and will approach similar problems from that standpoint in the future.<br><br>Michael<br><br>--- On <b>Sat, 8/14/10, Ben Millwood <i>&lt;haskell@benmachine.co.uk&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Ben Millwood &lt;haskell@benmachine.co.uk&gt;<br>Subject: Re: [Haskell-cafe] Unwrapping long lines in text files<br>To: "michael rice" &lt;nowgate@yahoo.com&gt;<br>Cc: haskell-cafe@haskell.org<br>Date: Saturday, August 14, 2010, 7:07
 PM<br><br><div class="plainMail">On Sat, Aug 14, 2010 at 2:38 AM, michael rice &lt;<a ymailto="mailto:nowgate@yahoo.com" href="/mc/compose?to=nowgate@yahoo.com">nowgate@yahoo.com</a>&gt; wrote:<br>&gt;<br>&gt; 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.<br>&gt;<br>&gt; How do I lose the EOF?<br>&gt;<br>&gt; Michael<br>&gt;<br><br>While many other people have shown you why you need not necessarily<br>answer this question, I think it'd be helpful for you to hear the<br>answer anyway.<br>Your message is being produced because you are trying to getLine when<br>there is no input left. This raises an exception, which, because it is<br>not handled by your program, prints a diagnostic message and exits.<br>Strangely, it prints this before the output of your program - this<br>isn't terribly important, but for the sake of completeness, it's<br>because of the different
 buffering characteristics of stdout and<br>stderr, which confusingly mean that even though your program produces<br>output and then produces an error, the error is printed immediately<br>while the output waits until the program is terminated to be produced.<br>I think. Something like that, anyway.<br><br>So, how do you avoid the exception? You can use System.IO.isEOF [1] to<br>check if there is input available on the standard input handle:<br><br>main = do<br>&nbsp; eof &lt;- isEOF<br>&nbsp; when (not eof) realMain<br>&nbsp; -- when from Control.Monad, see also: unless<br> where<br>&nbsp; realMain = do realStuff<br><br>Or you can let getLine throw the exception, but catch it and deal with<br>it yourself, rather than letting it kill your program.<br>Catching it is a little less simple, but is considerably more flexible<br>and powerful. The exceptions situation in Haskell is somewhat<br>complicated by the fact that the mechanism used by haskell98 has
 been<br>improved upon, but the new extensible mechanism is incompatible with<br>the old so both have to hang around. Have a look at Control.Exception<br>[2] and System.IO.Error [3]. In either case you have 'catch' as a sort<br>of basic operation, and more convenient things like 'try' which sort<br>of turn an exception into a pure Either result. I'd do something like<br>this:<br><br>main = do<br>&nbsp; result &lt;- try getLine<br>&nbsp; case result of<br>&nbsp; &nbsp; Left err -&gt; return () -- or do something diagnostic<br>&nbsp; &nbsp; Right "" -&gt; putStrLn "" &gt;&gt; main<br>&nbsp; &nbsp; Right line -&gt; doStuffWith line<br><br>On a more general note, your main function looks a little suspect<br>because it *always* recurses into itself - there's no termination<br>condition. A question nearly as important as "how does my program know<br>what to do" is "how does it know when to stop" :)<br><br>[1] <a
 href="http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/System-IO.html#v%3AisEOF" target="_blank">http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/System-IO.html#v%3AisEOF</a><br>[2] <a href="http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Control-Exception.html" target="_blank">http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Control-Exception.html</a><br>[3] <a href="http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/System-IO-Error.html" target="_blank">http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/System-IO-Error.html</a><br></div></blockquote></td></tr></table><br>