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
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
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]
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
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
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]
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
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.