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

ChrisK haskell at list.mightyreason.com
Sat Feb 9 08:19:22 EST 2008


For Bimap is there anything like Data.Map.insertWithKey ?

Stuart Cook wrote:
> 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