Y and Z combinators in Javascript — Lambda Calculus with real code

Enrico Piccinin
The Startup
Published in
7 min readJun 7, 2019

--

Can you do recursion using only Javascript anonymous functions?

Certainly. Here the Factorial of a number in one line

const fact = x => (x === 0 ? 1 : x * fact(x - 1));

No, no. This is not anonymous. You name the function fact via a Javascript const and reference it within itself. I mean really anonymous, something like

(...params) => { // do stuff }

with no variables at all, no self references, no state, just pure functions. Can you do this?

Well, according to Lambda Calculus you can, using what are called Fixed-Point Combinators.

Let’s see them in action using real Javascript code.

Lambda Calculus in Javascript terms

In a very simple way, we can say that a Lambda Calculus is based on the use of unary functions, which mean functions with one parameter. Lambda functions are expressed in the form of λx.y, where x is the variable of the function and y is its body. Things like λx.y are called Lambda abstractions in Lambda calculus theory.

Therefore the natural translation of Lambda abstractions to Javascript are anonymous functions. Some examples

--

--

Enrico Piccinin
The Startup

A man with passion for code and for some strange things that sometimes happen in IT organizations. Views and thoughts here are my own.