List functions - "Under" operations

Tomasz Zielonka t.zielonka at
Sat Jan 17 14:45:01 EST 2004

On Sat, Jan 17, 2004 at 08:24:45AM -0500, ajb at wrote:
> > Almost all of the times I use the "By" functions follow that pattern:
> > e.g.
> > sortFoos :: [Foo] -> [Foo]
> > sortFoos = sortBy (\x y -> compare (f x) (f y))
> Just noting that this particular example has a very simple solution:
> This technique probably doesn't solve all your problems, though.

It doesn't work when the type Foo doesn't belong to Ord class.

There is a simple technique which helped me when I wanted to make a
priority queue of elements with Double key, and IO () value.  I wanted
to use something from Edison, but most of relevant data structures
didn't distinguish key from value and insisted that element type belong
to Ord class.

I just introduced a wrapper type that provides a trivial "Egalite"

    newtype NoOrd a = NoOrd { unNoOrd :: a }

    instance Eq (NoOrd a) where
	_ == _ = True

    instance Ord (NoOrd a) where
	_ `compare` _ = EQ

Then I could store in the queue values of type (Double, NoOrd (IO ()))

Best regards,

.signature: Too many levels of symbolic links

More information about the Libraries mailing list