All pastes #747722 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-10-24 09:04 UTC
rendered paste body
import Data.List

encode :: (Eq a) => [a] -> [(Int, a)]
encode s = [(length run, head run) | run <- group s]

grabdigit :: [Char] -> ([Char], [Char])
grabdigit s
    | s == [] = ("", [])
    | head s >= '0' && head s <= '9' =
        let (num, rem) = grabdigit (tail s) in (head s : num, rem)
    | otherwise = ("", s)

decode :: [Char] -> [Char]
decode s
    | s == [] = ""
    | otherwise =
        let (num, rem) = grabdigit s in
            replicate (read num :: Int) (head rem) ++ decode (tail rem)

-- vim: ts=4 sw=4 et