Skip to content

Instantly share code, notes, and snippets.

@michaelfeathers
Created May 1, 2012 22:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michaelfeathers/2572083 to your computer and use it in GitHub Desktop.
Save michaelfeathers/2572083 to your computer and use it in GitHub Desktop.
Better
import Data.List
lineBreak :: String -> String
lineBreak = joinBrokenLines . joinWordsInBrokenLines . brokenLines . words
brokenLines :: [String] -> [[String]]
brokenLines [] = []
brokenLines wordList = brokenLine : brokenLines remainingWords
where (brokenLine, remainingWords) = splitAt (brokenLineWordCount wordList) wordList
brokenLineWordCount :: [String] -> Int
brokenLineWordCount = length . takeWhile (<= 13) . scanl (+) 0 . map wordLength
wordLength :: String -> Int
wordLength = succ . length
joinWordsInBrokenLines :: [[String]] -> [String]
joinWordsInBrokenLines = map (intercalate " ")
joinBrokenLines :: [String] -> String
joinBrokenLines = intercalate "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment