summaryrefslogtreecommitdiff
path: root/H10.hs
blob: 0694b6912b23ef9541b292de52be9130392c8170 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module H10
(   encode
) where

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