Created
February 3, 2013 04:08
-
-
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.
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.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