diff options
Diffstat (limited to 'h10.hs')
-rw-r--r-- | h10.hs | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -0,0 +1,20 @@ +import Control.Applicative +import Control.Arrow +import Data.List + +encode :: Eq a => [a] -> [(a,Int)] +encode = foldr elim [] + where + elim e [] = [(e,1)] + elim e p@(n:ns) + | e == fst n = (e,1 + (snd n)):ns + | otherwise = (e,1):p + +encode' (x:xs) = let (first,rest) = span (==x) xs + in (x,1+(length first)) : encode' rest + +encode' [] = [] + +encode'' xs = map (head &&& length) $ group xs + +encode''' xs = map ((,) <$> head <*> length) $ group xs |