Lambda Calculus Part 1: Curry
The lambda calculus is a calculus because describes how to manipulate expressions that describe computations; this makes it distinct from other calculi with which you may be more familiar (the differential calculus describes how to manipulate expressions that represent functions; propositional calculus describes how to manipulate expressions that represent propositions).
The lambda calculus itself essentially a very simple expression-based programming language that consists of exactly three types of expressions:
- An identifier reference: a name to identify an abstraction point.
- A function definition, which introduces an abstraction.
- A function application.
We’ll focus on function definition first, and one particular aspect of function definitions in lambda calculus that can be confusing at first.
A function in lambda calculus is notated like this: λparam.body. This expression defines a function with a single parameter.
This can be a little confusing at first - how useful could a language be if all your functions can only accept a single parameter? The answer is simple: we can create functions that look and behave as if they take multiple parameters. Taking a two parameter function and representing it with 2 one paramter functions is called currying.
Using lambda notation, we can write a curried addition function thus:
Simply put, the addition function is a function on one variable:
x. This function, when given
x returns the function that adds
Now, let’s translate that into ES6:
For comparison, here is the equivalent non-curried
Currying is a built-in feature of functional languages; in a language such as Haskell, all functions are curried.