summaryrefslogtreecommitdiff
path: root/11-20/11.hs
blob: a64fdc259870094e610988eac04fd2f926c2602f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
pack :: Eq a => [a] -> [[a]]

pack [] = []
pack [x] = [[x]]
pack (x:xs) = if x == head y then (x:y):ys else [x]:(y:ys) where (y:ys) = pack xs

data ListItem a = Single a | Multiple Int a deriving Show
encodeModified :: Eq a => [a] -> [ListItem a]

encodeModified xs = map (\x -> let l = length x
                                   h = head x in
                           case l of
                             1 -> Single h
                             otherwise -> Multiple l h) $ pack xs