99-haskell-problems/Problems 1-10/problem_10.hs

11 lines
254 B
Haskell

encode :: (Eq a, Num n) => [a] -> [(n, a)]
encode =
foldr
( \x acc -> case acc of
[] -> [(1, x)]
(hd@(hd_n, hd_a) : tl) ->
if x == hd_a
then (hd_n + 1, hd_a) : tl
else (1, x) : hd : tl
)
[]