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

Jed Brown jed at 59A2.org
Fri Feb 8 07:21:01 EST 2008

On  8 Feb 2008, news at lyra.net wrote:

> Hallo!
> Let's suppose I have a list [a,b,c,d,c,d]. I'd like to write
> a function that returns a new list without duplicates (in
> the example [a,b,c,d]). How can I do that? What is the most
> general way? I'd like to use the same function for a list of
> Int or String or some other user defined data type.

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)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20080208/5bd1e2ff/attachment.bin

More information about the Haskell-Cafe mailing list