Skip to content

Instantly share code, notes, and snippets.

@moreindirection
Created August 14, 2010 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moreindirection/524460 to your computer and use it in GitHub Desktop.
Save moreindirection/524460 to your computer and use it in GitHub Desktop.
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