Created
September 24, 2017 14:04
-
-
Save pzp1997/ab9cbba91928584963c26801facd7699 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
group :: Eq a => [a] -> [(a, Int)] | |
group = foldr step [] | |
where step c [] = [(c, 1)] | |
step c whole@((x, n) : xs) | |
| c == x = (x, n + 1) : xs | |
| otherwise = (c, 1) : whole | |
compress :: String -> String | |
compress = | |
concatMap f . group | |
where f (x, 1) = [x] | |
f (x, n) = [x] ++ show n | |
main :: IO () | |
main = do | |
input <- getLine | |
putStrLn $ compress input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment