
17 Jul
2007
17 Jul
'07
7:17 p.m.
on, which will appear in Data.Function in the next release of base, is defined thusly:
on :: (b -> b -> c) -> (a -> b) -> a -> a -> c (*) `on` f = \x y -> f x * f y
You can also use Data.Ord.comparing, in this case -- comparing is just (compare `on`).
From Ord.hs:
-- | -- > comparing p x y = compare (p x) (p y) -- -- Useful combinator for use in conjunction with the @xxxBy@ family -- of functions from "Data.List", for example: -- -- > ... sortBy (comparing fst) ... comparing :: (Ord a) => (b -> a) -> b -> b -> Ordering comparing p x y = compare (p x) (p y) Shachaf