[Haskell-cafe] Create a list without duplicates from a list with duplicates

Felipe Lessa felipe.lessa at gmail.com
Fri Feb 8 07:25:19 EST 2008


2008/2/8 Jed Brown <jed at 59a2.org>:
> Look at Data.List:
>
> nub :: (Eq a) => [a] -> [a]
> nub = nubBy (==)
>
> nubBy :: (a -> a -> Bool) -> [a] -> [a]
> nubBy eq []     = []
> nubBy eq (x:xs) = x : nubBy eq (filter (\ y -> not (eq x y)) xs)

And then there's also

sort :: (Ord a) => [a] -> [a]

which should have better performance, O(n log n) against O(n²) I
guess, but of course will change the order of the elements. If you
really don't mind the order at all, you could also use Data.Set in the
first place.

Cheers,

-- 
Felipe.


More information about the Haskell-Cafe mailing list