The nub
function from Data.List
(no, it's actually not in
the Prelude) definitely does something like what you want, but it is not quite the
same as your unique
function. They both preserve the original
order of the elements, but unique
retains the last
occurrence of each element, while nub
retains the first occurrence.
You can do this to make nub
act exactly like unique
,
if that's important (though I have a feeling it's not):
unique = reverse . nub . reverse
Also, nub
is only good for small lists.
Its complexity is quadratic, so it starts to get slow if your list can contain hundreds
of elements.
If you limit your types to types having an Ord instance, you can make it scale better.
This variation on nub
still preserves the order of the list elements,
but its complexity is O(n * log n)
:
import qualified Data.Set as Set
nubOrd :: Ord e => [a] -> [a]
nubOrd xs = go Set.empty xs where
go s (x:xs)
| x `Set.member` s = go s xs
| otherwise = x : go (Set.insert x s) xs
go _ _ = []
In fact, it has been proposed
to add nubOrd
to Data.Set
.