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

2008-02-11 Thread Dan Weston
Of course the most *general* way requires an Eq constraint: > List.nub :: Eq a => [a] -> [a] But there are better functions (already mentioned) with the less general Ord constraint. Int and String are instances of Ord. "some other user defined data type" probably is too, but if you mean "an

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

2008-02-09 Thread Henning Thielemann
On Fri, 8 Feb 2008, [EMAIL PROTECTED] 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 l

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

2008-02-08 Thread Stuart Cook
On Sat, Feb 9, 2008 at 7:36 AM, Dan Weston <[EMAIL PROTECTED]> wrote: > If order is important, the new bijective Data.Bimap class > http://code.haskell.org/~scook0/haddock/bimap/Data-Bimap.html > may be your best bet (I haven't yet tried it myself). Let me try: nub :: (Ord a) => [a] -> [a]

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

2008-02-08 Thread Tillmann Rendel
Dan Weston wrote: Meanwhile, here is a hand-rolled solution to order-preserving nubbing: > import Data.List(groupBy,sortBy,sort) > import Data.Maybe(listToMaybe) > > efficientNub :: (Ord a) => [a] -> [a] > efficientNub = flip zip [0..]-- carry along index > >>> sort

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

2008-02-08 Thread Dan Weston
As noted, (Data.Set.toList . Data.Set.fromList) is the best traditional solution if you don't care about order (or Data.Set.toAscList for a sorted result). If order is important, the new bijective Data.Bimap class http://code.haskell.org/~scook0/haddock/bimap/Data-Bimap.html may be your best be

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

2008-02-08 Thread Felipe Lessa
2008/2/8 Jed Brown <[EMAIL PROTECTED]>: > 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]

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

2008-02-08 Thread Jed Brown
On 8 Feb 2008, [EMAIL PROTECTED] 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 o

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

2008-02-08 Thread [EMAIL PROTECTED]
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.