Created
May 27, 2012 02:58
-
-
Save xerxesb/2798861 to your computer and use it in GitHub Desktop.
Xerxes solutions to Run-Length-Encoding problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List (group) | |
data Segment x = Single x | Multiple Int x | |
deriving (Show, Eq) | |
encodeModified :: (Eq a) => [a] -> [Segment a] | |
--encodeModified xs = map (\charset -> | |
-- if ((length charset) == 1) | |
-- then (Single (head charset)) | |
-- else (Multiple (length charset) (head charset))) (group xs) | |
--encodeModified = map func . group | |
-- where func = (\charset -> | |
-- if ((length charset) == 1) | |
-- then (Single (head charset)) | |
-- else (Multiple (length charset) (head charset))) | |
tupleIt :: Eq a => [a] -> [(Int, a)] | |
tupleIt xs = map (\x -> (length x, head x)) (group xs) | |
encodeModified xs = map encodeHelper (tupleIt xs) | |
where | |
encodeHelper (1, x) = Single x | |
encodeHelper (n, x) = Multiple n x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment