
Right, but if you use Prelude.interact, as in my example, you don't have to worry about the EOF checking yourself - it's all handled for you. And if you use a functional style, your code will be simpler and more testable - otherwise you might as well just use an imperative language. Here's a slightly better version (uses splitAt): maxLineLength :: Int maxLineLength = 72 wrapLine :: String -> [String] wrapLine "" = [] wrapLine line | length line <= maxLineLength = [line] | otherwise = let (line, rest) = splitAt maxLineLength line in line : wrapLine rest main :: IO () main = interact $ unlines . concatMap wrapLine . lines On Saturday Aug 14, 2010, at 12:38 AM, michael rice wrote:
Hi Bill,
Each quote of the input is on a single line. I want to unwrap lines greater than 72 characters, i.e., break them into several lines each <= 72 characters, but I don't want to break up words. and I want to retain the blank lines. So, my output is correct except for the error message.
Michael
============ My input data ============ However mean your life is, meet it and live it: do not shun it and call it hard names. Cultivate poverty like a garden herb, like sage. Do not trouble yourself much to get new things, whether clothes or friends. Things do not change, we change. Sell your clothes and keep your thoughts. God will see that you do want society.
Men have become the tools of their tools.
I know of no more encouraging fact than the unquestioned ability of a man to elevate his life by conscious endeavor.
I once had a sparrow alight upon my shoulder for a moment, while I was hoeing in a village garden, and I felt that I was more distinguished by that circumstance that I should have been by any epaulet I could have worn.
-Thoreau ============ My output ============ unwrap: <stdin>: hGetLine: end of file <<<< here's my eof message However mean your life is, meet it and live it: do not shun it and call it hard names. Cultivate poverty like a garden herb, like sage. Do not trouble yourself much to get new things, whether clothes or friends. Things do not change, we change. Sell your clothes and keep your thoughts. God will see that you do want society.
Men have become the tools of their tools.
I know of no more encouraging fact than the unquestioned ability of a man to elevate his life by conscious endeavor.
I once had a sparrow alight upon my shoulder for a moment, while I was hoeing in a village garden, and I felt that I was more distinguished by that circumstance that I should have been by any epaulet I could have worn.
-Thoreau ============ Your output ========== However mean your life is, meet it and live it: do not shun it and call it hard names. Cultivate poverty like a garden herb, like sage. Do not t rouble yourself much to get new things, whether clothes or friends. Thin gs do not change, we change. Sell your clothes and keep your thoughts. G od will see that you do want society. Men have become the tools of their tools. I know of no more encouraging fact than the unquestioned ability of a ma n to elevate his life by conscious endeavor. I once had a sparrow alight upon my shoulder for a moment, while I was h oeing in a village garden, and I felt that I was more distinguished by t hat circumstance that I should have been by any epaulet I could have wor n. -Thoreau ===================================
--- On Fri, 8/13/10, Bill Atkins
wrote: From: Bill Atkins
Subject: Re: [Haskell-cafe] Unwrapping long lines in text files To: "michael rice" Cc: haskell-cafe@haskell.org Date: Friday, August 13, 2010, 11:13 PM Not sure if I understood what you're trying to do, but development will be easier if you minimize your IO, e.g. :
maxLineLength :: Int maxLineLength = 72
wrapLine :: String -> [String] wrapLine "" = [] wrapLine line | length line <= maxLineLength = [line] | otherwise = take maxLineLength line : wrapLine (drop maxLineLength line)
main :: IO () main = interact $ unlines . concatMap wrapLine . lines
Now wrapLine is pure and you can use it more easily using GHCi. Removing dependencies on IO usually makes your problem easier to test and understand and your code simpler.
In your example, the EOF probably happens on the call to getLine after input has run out. By using Prelude.interact, we can ignore details like that and rely on already-written functions.
HTH, Bill
On Friday Aug 13, 2010, at 9:38 PM, michael rice 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?
Michael
====== unwrap.hs ======
main = do line <- getLine if null line then do putStrLn "" main else do printList (words line) 1 main
printList :: [String] -> Int -> IO () printList [] _ = do putStrLn "" printList (w:[]) k = do if k+(length w) <= 72 then do putStrLn w else do putStrLn "" putStrLn w printList r@(w:ws) k = do if k+(length w) <= 72 then do putStr w putStr " " printList ws (k+(length w)+1) else do putStrLn "" printList r 1
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe