Skip to content

Instantly share code, notes, and snippets.

@geowa4
Created February 3, 2013 04:08
Show Gist options
  • Save geowa4/4700518 to your computer and use it in GitHub Desktop.
Save geowa4/4700518 to your computer and use it in GitHub Desktop.
Take a string and split it over multiple lines of a specified max length. If a word is too big to fit on a single line, just put it on its own line for simplicity.
import Data.List
makeLines :: Int -> String -> String
makeLines len = (intercalate "\n") . (foldl (buildLine len) [""]) . words
where
buildLine :: Int -> [String] -> String -> [String]
buildLine len currLines word =
if length newLine > len
then currLines ++ [word]
else ls ++ [newLine]
where
ls = init currLines
l = last currLines
newLine = dropWhile (==' ') $ l ++ " " ++ word
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment