Skip to content

Instantly share code, notes, and snippets.

@pzp1997
Created September 24, 2017 14:04
Show Gist options
  • Save pzp1997/ab9cbba91928584963c26801facd7699 to your computer and use it in GitHub Desktop.
Save pzp1997/ab9cbba91928584963c26801facd7699 to your computer and use it in GitHub Desktop.
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