Skip to content

Instantly share code, notes, and snippets.

@5outh
Created July 11, 2012 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5outh/3093214 to your computer and use it in GitHub Desktop.
Save 5outh/3093214 to your computer and use it in GitHub Desktop.
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