Difference between revisions of "99 questions/Solutions/12"
< 99 questions | Solutions
Jump to navigation
Jump to search
Jargonjustin (talk | contribs) |
|||
Line 12: | Line 12: | ||
We only need to map single instances of an element to a list containing only one element and multiple ones to a list containing the specified number of elements and concatenate these lists. |
We only need to map single instances of an element to a list containing only one element and multiple ones to a list containing the specified number of elements and concatenate these lists. |
||
+ | |||
+ | A solution for the simpler encoding (as <haskell>[(Int, a)]</haskell>) from problem 10 can be given as: |
||
+ | |||
+ | <haskell> |
||
+ | decode :: [(Int, a)] -> [a] |
||
+ | decode = concatMap (uncurry replicate) |
||
+ | </haskell> |
Revision as of 05:12, 11 May 2011
(**) Decode a run-length encoded list.
Given a run-length code list generated as specified in problem 11. Construct its uncompressed version.
decodeModified :: [ListItem a] -> [a]
decodeModified = concatMap decodeHelper
where
decodeHelper (Single x) = [x]
decodeHelper (Multiple n x) = replicate n x
We only need to map single instances of an element to a list containing only one element and multiple ones to a list containing the specified number of elements and concatenate these lists.
A solution for the simpler encoding (as[(Int, a)]
decode :: [(Int, a)] -> [a]
decode = concatMap (uncurry replicate)