Skip to content

Instantly share code, notes, and snippets.

@jszmajda
Created February 4, 2016 13:53
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 jszmajda/db08981797b240721352 to your computer and use it in GitHub Desktop.
Save jszmajda/db08981797b240721352 to your computer and use it in GitHub Desktop.
Polyglot Programming DC Randori of WordWrap function
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