Created
July 11, 2012 20:38
-
-
Save 5outh/3093214 to your computer and use it in GitHub Desktop.
99 Haskell Problems: Problem 13
This file contains hidden or 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
data Count = Multiple Int Char | |
| Single Char | |
deriving (Eq, Show) | |
encodeDirect :: [Char] -> [Count] | |
encodeDirect = map toCount . foldr encode [] | |
where | |
toCount (x, y) = case x of | |
1 -> Single y | |
_ -> Multiple x y | |
encode x [] = [(1, x)] | |
encode x (y@(a, b):ys) | |
| b == x = (a+1, b):ys | |
| otherwise = (1, x):y:ys |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment