Skip to content

Instantly share code, notes, and snippets.

@5outh
Created Jul 11, 2012
Embed
What would you like to do?
99 Haskell Problems: Problem 13
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