haskellcombinators

Haskell: Is there a simpler way to express the function (\f g x y -> f (g x) (g y))? Using Applicative ((->) r)?


combinator :: (b -> b -> c) -> (a -> b) -> a -> a -> c
combinator f g x y = f (g x) (g y)

Is there a simpler way to express this function? I find myself using it quite frequently.

I tried using the Applicative instance for (r->) but the best I could get was (<*>) : (r -> a -> b) -> (r -> a) -> (r -> b), f <*> g = (\r -> f r (g r))


Solution

  • You're obviously not the only one to find that useful, because this combinator included in base, in the Data.Function module. It's called on.

    The Hoogle search engine allows searching for functions by their signature, and is helpful in finding the answer to questions like these.