Run-length encoding: Difference between revisions

Content added Content deleted
(→‎Version 2: improvement)
Line 1,686: Line 1,686:


runLengthEncode = concatMap (\xs@(x:_) -> (show.length $ xs) ++ [x]).group
runLengthEncode = concatMap (\xs@(x:_) -> (show.length $ xs) ++ [x]).group
runLengthDecode = concat.uncurry (zipWith (\[x] ns -> replicate (read ns) x))

runLengthDecode = concat.reverse.snd.foldl go (0,[]).groupBy (\a b -> isDigit a && isDigit b)
.foldr (\z (x,y) -> (y,z:x)) ([],[]).groupBy (\x y -> all isDigit [x,y])
where
go (n,ls) [x] | not.isDigit $ x = (0,replicate n x : ls)
go (n,ls) ns = (read ns,ls)


main = do
main = do