[Haskell-cafe] Create a list without duplicates from a list with
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
> 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!"
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.
More information about the Haskell-Cafe