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

12 lines
254 B
Haskell
Raw Normal View History

2024-05-24 17:06:42 -05:00
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
)
[]