Created
November 29, 2012 13:49
-
-
Save dmiyakawa/4169204 to your computer and use it in GitHub Desktop.
Compress 2
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 Control.Arrow | |
-- "aaabbbbcc" | |
-- ["aaa", "bbbb", "cc"] | |
f1 :: String -> [String] | |
f1 str = group str | |
-- "aaa" | |
-- -> ("a", "3") | |
f2 :: [Char] -> (Char, String) | |
f2 xs = (head &&& (show . length)) xs | |
-- ("a", "13") | |
-- -> "a13" | |
f3 :: (Char, String) -> String | |
f3 (a, b) = uncurry (:) (a, b) | |
-- "aaaa" | |
-- "a4" | |
f23 :: [Char] -> String | |
f23 xs = f3 $ f2 xs | |
f4 :: (String -> String) -> [String] -> String | |
f4 f xs = concat $ map f xs | |
compress :: String -> String | |
compress s = f4 f23 $ f1 s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment