[Haskell-cafe] Is this a useful higher-order function,
or should I RTFM?
Tom Pledger
tpledger at ihug.co.nz
Wed Dec 8 04:13:22 EST 2004
Steven Huwig wrote:
>
> On Dec 6, 2004, at 11:05 PM, Tom Pledger wrote:
>
>> import Data.Char(isSpace)
>> import Data.List(groupBy)
>>
>> (op `on` f) x y = f x `op` f y
>> wordsAndSpaces = groupBy ((==) `on` isSpace)
>>
>
> `on` is a handy little function in this instance.
>
> Does it have a technical name?
Perhaps, but I don't know it, and wanted to make the code sound OK when
read aloud. :-)
> Have you used it elsewhere?
Yes, a few times, mainly as (compare `on` fst), as in:
-- Apply to xs the permutation that would stably sort ks
unpermute ks xs
= map snd (sortBy (compare `on` fst) (zip ks xs))
Regards,
Tom
More information about the Haskell-Cafe
mailing list