Created
February 4, 2016 13:53
-
-
Save jszmajda/db08981797b240721352 to your computer and use it in GitHub Desktop.
Polyglot Programming DC Randori of WordWrap function
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
wordWrap :: String -> Int -> String | |
wordWrap xs maxLineLength = wordWrapHelper "" xs maxLineLength maxLineLength | |
wordWrapHelper :: String -> String -> Int -> Int -> String | |
wordWrapHelper result xs maxLineLength index | |
| length xs < maxLineLength + 1 = result ++ xs | |
| xs !! index == ' ' = wordWrapHelper (result ++ firstPart) lastPart maxLineLength maxLineLength | |
| otherwise = wordWrapHelper result xs maxLineLength (index - 1) | |
where | |
firstPart = (take index xs) ++ "\n" | |
lastPart = drop (index + 1) xs | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment