Revision: 49755
Updated Code
at July 29, 2011 08:06 by rtperson
Updated Code
data EncodedElement a = Multiple Int a | Single a deriving Show encodeModified :: (Eq a) => [a] -> [EncodedElement a] encodeModified = map (\x -> if (length x > 1) then Multiple (length x) (head x) else Single (head x)) . group decodeModified :: [EncodedElement a] -> [a] decodeModified = concatMap decode where decode (Multiple n c) = replicate n c decode (Single c) = c : []
Revision: 49754
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 29, 2011 08:03 by rtperson
Initial Code
data EncodedElement a = Multiple Int a | Single a deriving Show encodeModified :: (Eq a) => [a] -> [EncodedElement a] encodeModified = map (\x -> if (length x > 1) then Multiple (length x) (head x) else Single (head x)) . group isMultiple :: EncodedElement a -> Bool isMultiple x = case x of Multiple _ _ -> True Single _ -> False decodeModified :: [EncodedElement a] -> [a] decodeModified = concatMap decode where decode (Multiple n c) = replicate n c decode (Single c) = c : []
Initial URL
http://www.haskell.org/haskellwiki/99_questions/11_to_20
Initial Description
problem 11, modified run-length encoding ======================================== If an element has no duplicates, mark it as such. Example: encodeModified "aaaabccaadeeee" [Multiple 4 'a',Single 'b',Multiple 2 'c', Multiple 2 'a',Single 'd',Multiple 4 'e'] problem 12, Decode a run-length encoded list. ============================================= Given a run-length code list generated as specified in problem 11. Construct its uncompressed version. Example: decodeModified [Multiple 4 'a',Single 'b',Multiple 2 'c', Multiple 2 'a',Single 'd',Multiple 4 'e'] "aaaabccaadeeee"
Initial Title
Haskell 99 Problems - Solutions to 11 and 12
Initial Tags
Initial Language
Haskell