Functional Programming – Difference Between a Combinator and Function Chaining

functional programmingmath

So from what I've read about combinators I can't quite tell how they're different from simply chaining function calls. I know I must be missing something but I'm not figuring out what I'm missing. I mean would g(f(x)) (where f and g are functions) effectively be a combinator if I gave it a different name?

Best Answer

Function chaining is known as function composition in the functional world.

A combinator however is a completely different thing, the composition operator is simply one combinator.

I wrote a blog attempting to explain combinators here it may help.

In it I try to explain a combinator is simply a function that follows a few rules in the general case, but in the common case "combinator" is used to refer to a function which abstracts a bit of common functionality that is very basic in form.

in SKI combinator calculus there are 3 combinators: Kxy = x Sxyz = xz(yz) Ix = x

These are combinators because they act only upon their terms, and they are generalized to not care what their terms are.

This seems useless but realize I can be defined in terms of S and K: SKKx = Kx(Kx) = x which is equivalent to Ix which = x, the benefit to combinators is they are fully generalized to whatever terms you pass them and therefore live at a higher level abstraction than other things.

The composition combinator is the B combinator from the B,C,K,W combinator system

Now to make combinators useful however, you have to understand when fully generalized functions like this are worth creating. Parsers are the perfect and most common example, because a parser as a concept has to deal with an extremely large number of possible inputs, therefore the ability to generalize the handling of their inputs comes in very handy.

Related Topic