Last active
March 25, 2019 12:54
-
-
Save dnery/c84f1678506ef9cdc8e8fb695da8076a to your computer and use it in GitHub Desktop.
dailyprogrammer #279 solution attempt in haskell
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 | |
import Data.List.Utils -- cabal install missingH | |
-- solution.hs | |
-- reflow and justify input text to 40-char width. | |
-- redir the output to a formatting function, so | |
-- results can actually be seen. e.g.: | |
-- putStrLn(prettify "sample text") | |
-- curryfying function | |
prettify :: [Char] -> [Char] | |
prettify [] = [] | |
prettify a = concat(justify(reflow(words a))) | |
-- reflow word list function | |
reflow:: [[Char]] -> [[Char]] | |
reflow a@(_:[]) = a | |
reflow a@(x:y:_) | |
| length(x ++ " " ++ y) <= 40 = reflow((x ++ " " ++ y):tail(tail a)) | |
| otherwise = (x ++ "\n"):[] ++ reflow(tail a) | |
-- justify line list function | |
justify :: [[Char]] -> [[Char]] | |
justify a@(_:[]) = a | |
justify a@(x:_) | |
| (length(findIndices(== ' ') x) + (length x) <= 40) | |
= justify((replace " " " " x):(tail a)) | |
| otherwise = x:[] ++ justify(tail a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment