Write biSp in point-free form using as few terms as possible The Haskell function biSp has type signature

biSp :: (a -&gt; c) -&gt; (b -&gt; d) -&gt; (c -&gt; d -&gt; e) -&gt; a -&gt; b -&gt; e


and (for those who prefer combinator calculus) can be defined as

biSp g h f x y = f (g x) (h y)


Your task is to implement biSp in point-free form (equivalently: as a combinator without any lambdas) using only two primitives:

(.) :: (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c
flip :: (a -&gt; b -&gt; c) -&gt; b -&gt; a -&gt; c


or

(.) f g x = f (g x)
flip f x y = f y x


For those with a background in combinator calculus, these are respectively the B and C combinators.

You may define helper functions so long as they are point-free. Your score is the total number of terms in all right-hand-side expressions.

Testing

You can test a Haskell solution without installing any software using Ideone. By providing an explicit type alongside the definition you ensure a compile-time error if the function is incorrect. E.g. using the :pl reference implementation (online demo):

biSp :: (a -&gt; c) -&gt; (b -&gt; d) -&gt; (c -&gt; d -&gt; e) -&gt; a -&gt; b -&gt; e
biSp = flip . ((flip . ((.) .)) .) . flip (.)
main = putStrLn "Compiled ok"
7 terms

We again define a helper combinator BBQ = B B (C B), which has reduction rule

BBQ w x y z = x y (w z)


Then biSp = BBQ BBQ BBQ

  BBQ BBQ BBQ g h f x y
= BBQ g (BBQ h) f x y
= BBQ h f (g x) y
= f (g x) (h y)


Online demo

By brute force, this is the only 7-term solution and there is no smaller solution.

8 terms

There are a handful of 8-term solutions. Firstly, the obvious

C BBQ BBQ BBQ


which reduces in one step to BBQ BBQ BBQ.

Then there are three solutions of the form X X X with a five-term X:


X = B (B B) C B which reduces in one step to BBQ
X = C B (C B) B which reduces in one step to BBQ and allows the further extraction, for no net gain or loss, of a helper combinator Q = C B
X = C (B C (B B))


There is one solution of the form X X with a six-term X:


X = C B (B B (C B)) (again, with an equivalent version which extracts Q = C B)


And there is one solution which can only be written in 8 terms with two helper combinators:

Q = C B
X = Q (Q Q B)
biSp = X X