Is there a Haskell combinator of type `(a -> a -> b) -> (b -> b -> c) -> a -> a -> c`?

98 Views Asked by At

Does the following combinator have a name in Haskell? If not, can it be expressed in terms of simpler ones?

c f g x y = g (f x y) (f y x)

I have tried Hoogle and pointfree.io (which gives me back an unintelligible expression) but without success.

1

There are 1 best solutions below

0
willeM_ Van Onsem On

There is no known combinator for this as far as I know. We can however shorten this - with the help of @chepner - to:

import Control.Applicative(liftA2)
import Control.Monad(ap)

ourCombinator :: (b -> b -> c) -> (a -> a -> b) -> a -> a -> c
ourCombinator g = curry . ap (liftA2 g . uncurry) (uncurry . flip)

The uncurry :: (a -> b -> c) -> (a, b) -> c converts a function with two parameters to one that takes one parameter: a 2-tuple with the two parameters.

We then use liftA2 g that will apply g on the result of uncurry f and uncurry (flip f), and then we curry :: ((a, b) -> c) -> a -> b -> c to convert it back to a function that takes two parameters.