haskellfunctional-programming

Which is the most idiomatic way to "lift up" by some transformation both arguments of a binary function in Haskell?


Which is the most idiomatic way to "lift up" by some transformation both arguments of a binary function in Haskell? Let this operator be named "lift", so I expect it's type will be

lift :: (a -> b) -> (b -> b -> c) -> (a -> a -> c)

and a naive definition will be

lift t f = \x y -> f (t x) (t y)

Solution

  • It's called on (from Data.Function), although with flipped arguments:

    on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
    -- lift = flip on
    

    Note that you could have found the function easily with a Hoogλe query. Also note that there's already a function lift, which is used in a completely other setting, namely monad transformers.