[Haskell-beginners] List -> Map -> List, to add duplicates?

Ertugrul Soeylemez es at ertes.de
Tue Jun 7 09:18:25 CEST 2011


Tom Murphy <amindfv at gmail.com> wrote:

>      This seems like an inefficient way to do what I'm trying to do.
> I'd really appreciate any suggestions or comments:
>
> [...]

I don't see why it would be inefficient, although you don't exactly need
the map data structure.  A set would totally suffice, and since tuples
have an Ord instance, the following should work (untested!):

    import qualified Data.Set as S

    newtype Humanism a = Human { animal :: a }
    instance Eq (Humanism a) where _ == _ = True
    instance Ord (Humanism a) where compare _ _ = EQ

    swap :: (a,b) -> (b,a)
    swap (x,y) = (y,x)

    nubBySnd :: Ord b => [(a,b)] -> [(a,b)]
    nubBySnd =
        map (swap . first animal) .
        S.toList . S.fromList .
        map (first Human . swap)


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/





More information about the Beginners mailing list