Created
August 14, 2010 16:31
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Char (isSpace) | |
trim :: String -> String | |
trim = trimAndReverse . trimAndReverse | |
where trimAndReverse = reverse . dropWhile isSpace | |
reverseBreak :: (a -> Bool) -> [a] -> ([a], [a]) | |
reverseBreak f xs = (reverse before, reverse after) | |
where (after, before) = break f $ reverse xs | |
wrapLine :: Int -> String -> [String] | |
wrapLine maxLen line | |
| length line <= maxLen = [line] | |
| any isSpace beforeMax = beforeSpace : (wrapLine maxLen $ afterSpace ++ afterMax) | |
| otherwise = beforeMax : wrapLine maxLen afterMax | |
where (beforeMax, afterMax) = splitAt maxLen line | |
(beforeSpace, afterSpace) = reverseBreak isSpace beforeMax | |
main :: IO () | |
main = interact $ unlines . concatMap (map trim . wrapLine maxLen) . lines | |
where maxLen = 72 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment