{- |
Description: Eliminate duplicate elements in a list
Copyright: Copyright (C) 2021 Yoo Chung
License: GPL-3.0-or-later
Maintainer: dev@chungyc.org

Some solutions to "Problems.P08" of Ninety-Nine Haskell "Problems".
-}
module Solutions.P08 (compress) where

-- | Eliminate consecutive duplicates of list elements.
--
-- If a list contains repeated elements,
-- they should be replaced with a single copy of the element.
-- The order of the elements should not be changed.
compress :: Eq a => [a] -> [a]
compress :: forall a. Eq a => [a] -> [a]
compress (a
x:ys :: [a]
ys@(a
y:[a]
_))
  | a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y    = [a] -> [a]
forall a. Eq a => [a] -> [a]
compress [a]
ys
  | Bool
otherwise = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a]
forall a. Eq a => [a] -> [a]
compress [a]
ys
compress [a]
l = [a]
l