## Posted By

rtperson on 07/29/11

# Haskell 99 Problems - Solutions to 11 and 12

/ Published in: Haskell   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']

`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 : []`