[Haskell-cafe] Unwrapping long lines in text files
michael rice
nowgate at yahoo.com
Sat Aug 14 13:20:46 EDT 2010
Hi Bill,
Very clever.
You are an inspiration.
Michael
--- On Sat, 8/14/10, Bill Atkins <watkins at alum.rpi.edu> wrote:
From: Bill Atkins <watkins at alum.rpi.edu>
Subject: Re: [Haskell-cafe] Unwrapping long lines in text files
To: "michael rice" <nowgate at yahoo.com>
Cc: "Felipe Lessa" <felipe.lessa at gmail.com>, haskell-cafe at haskell.org
Date: Saturday, August 14, 2010, 12:33 PM
Try this one (http://gist.github.com/524460):
import Data.Char
maxLineLength :: Int
maxLineLength = 72
trim :: String -> String
trim = reverse . dropSpaces . reverse . dropSpaces
where dropSpaces = dropWhile isSpace
none :: (a -> Bool) -> [a] -> Bool
none f = not . any f
reverseBreak :: (a -> Bool) -> [a] -> ([a], [a])
reverseBreak f xs = (reverse before, reverse after)
where (after, before) = break f $ reverse xs
wrapLine :: String -> [String]
wrapLine "" = [[]]
wrapLine line
| length line <= maxLineLength = [line]
| none isSpace line = beforeMax : wrapLine afterMax
| otherwise = beforeSpace : (wrapLine $ afterSpace ++ afterMax)
where (beforeMax, afterMax) = splitAt maxLineLength line
(beforeSpace, afterSpace) = reverseBreak isSpace beforeMax
main :: IO ()
main = interact $ unlines . concatMap (map trim . wrapLine) . lines
On Saturday Aug 14, 2010, at 9:41 AM, michael rice wrote:
> Hi Filipe, Bill
>
> Your corrected version works, while the original didn't, but it still produces incorrect
> 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
>
> I don't want to break lines in the middle of words and I want to retain the original structure of the text with respect to blank lines between individual quotes. THe only thing in the input text that should change are the lines longer than 72 characters, and they should be reformatted to one or more lines less than or equal to 72 characters.
>
> Michael
>
> --- On Sat, 8/14/10, Felipe Lessa <felipe.lessa at gmail.com> wrote:
>
> From: Felipe Lessa <felipe.lessa at gmail.com>
> Subject: Re: [Haskell-cafe] Unwrapping long lines in text files
> To: "Bill Atkins" <watkins at alum.rpi.edu>
> Cc: "michael rice" <nowgate at yahoo.com>, haskell-cafe at haskell.org
> Date: Saturday, August 14, 2010, 9:17 AM
>
> On Sat, Aug 14, 2010 at 9:59 AM, Bill Atkins <watkins at alum.rpi.edu> wrote:
> > | otherwise = let (line, rest) = splitAt maxLineLength line in
> > line : wrapLine rest
>
> I haven't tested myself, but does this work at all? If I am reading
> it correctly, this is the same as
>
> let (foo, rest) = splitAt maxLineLength foo
> in foo : wrapLine rest
>
> In other words, no mention of wrapLine's argument 'line', and a
> recursive call that will bottom out and be the same as 'undefined' :).
> GHC would warn you, though, if you used -Wall. That expression
> should read:
>
> let (thisLine, rest) = splitAt maxLineLength line
> in thisLine : wrapLine rest
>
> Cheers,
>
> --
> Felipe.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100814/e680026c/attachment.html
More information about the Haskell-Cafe
mailing list