Skip to content

Instantly share code, notes, and snippets.

@xerxesb
Created May 27, 2012 02:58
Show Gist options
  • Save xerxesb/2798861 to your computer and use it in GitHub Desktop.
Save xerxesb/2798861 to your computer and use it in GitHub Desktop.
Xerxes solutions to Run-Length-Encoding problems
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