blob: 68ffadef1d57bf99ffd6d76ece478a9e3816059c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
|