You are viewing a single comment's thread. Return to all comments →
Haskell foldl' based:
import Data.List (foldl') compress = (foldl' format "") . (foldl' count []) where format str (c, 1) = c : str format str (c, num) = (c : show num) ++ str count (x:xs) c | c == prev = (c, num + 1) : xs | otherwise = (c, 1) : (prev, num) : xs where (prev, num) = x count xs c = (c, 1) : xs main = interact compress
Haskell group based:
import Data.List (concatMap, group) compress = (concatMap format) . group where format [x] = [x] format xs = (head xs) : (show . length) xs main = interact compress
Seems like cookies are disabled on this browser, please enable them to open this website
String Compression
You are viewing a single comment's thread. Return to all comments →
Haskell foldl' based:
Haskell group based: