summaryrefslogtreecommitdiff
path: root/11-20/13.hs
blob: db5509ef3562a2795e58cdf5d7b8cfa485ca1617 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
data ListItem a = Single a | Multiple Int a deriving Show
encodeDirect :: Eq a => [a] -> [ListItem a]

encodeDirect [] = []
encodeDirect [x] = [Single x]
encodeDirect (x:xs) = if x == h then n:ys else (Single x):(y:ys)
    where (y:ys) = encodeDirect xs
          helper (Single z) = (Multiple 2 z, z)
          helper (Multiple c z) = (Multiple (c + 1) z, z)
          (n, h) = helper y