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

Stuart Cook scook0 at gmail.com
Fri Feb 8 22:35:14 EST 2008


On Sat, Feb 9, 2008 at 7:36 AM, Dan Weston <westondan at imageworks.com> 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]
  nub = map snd . Data.Bimap.toAscList . Data.Bimap.fromList . reverse
. zip [1..]

  > nub "hello, world!"
  "helo, wrd!"

Without the call to (reverse), this would still be an order-preserving
nub, except that it would preserve the relative order of the *last*
occurrence of each element. Actually, this makes me wonder whether
fromList's behaviour should be changed, and whether I should add a
"non-clobbering" variant of insert.


Stuart


More information about the Haskell-Cafe mailing list