Skip to content

Instantly share code, notes, and snippets.

@dmiyakawa
Created November 29, 2012 13:49
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 dmiyakawa/4169204 to your computer and use it in GitHub Desktop.
Save dmiyakawa/4169204 to your computer and use it in GitHub Desktop.
Compress 2
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