Lambda Functions in Python
- First I looked at the following article from realpython.com
History
Lambda calculus (or $\lambda$-calculus) is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution.1
It is a universal model of computation. Alternative to a Turing Machine.
Until the 1960s, the lambda calculus was only a formalism. Since Richard Montague and other linguists’ applied it in understanding the semantics of natural language, the lambda calculus has gained popularity in both linguistics, and computer science.
Computable functions2 are a fundamental concept within computer science and mathematics. The lambda calculus provides a simple semantics for computation, enabling properties of computation to be studied formally.
Lambda calculus simplifies the semantics in two ways:
Lambda calculus treats functions “anonymously”, without giving them explicit names. For example:
square_sum$(x,y) = x^2 + y^2$
can be turned anonymous form by rewriting it as
$(x,y) \mapsto x^2 + y^2$
You now think of it as a mapping of the tuple $x$, and $y$ to $x^2+y^2$.
Lambda calculus only uses functions of a single input. For instance, the square_sum function is equivalent to the functions
$x \mapsto (y \mapsto x^2+ y^2)$
This method is known as currying. It transforms a function that takes multiple arguments into a chain of functions each with a single argument.
We understand this approach to functions by calculating square_sum at $(5,2)$.
$((x,y) \mapsto x^2 + y^2)(5,2)$ $= 5^2 +2^2$ $=29$
On the other hand, the curried version requires an additional step.
$\left( \left( x \mapsto (y \mapsto x^2 + y^2) \right)(5) \right)(2)$ $= (y \mapsto 5^2 + y^2)(2)$ $= 5^2 + 2^2$ $= 29$
The lambda calculus consists of a language of lambda terms, which is defined by a certain formal syntax, and a set of transformation rules, which allow manipulation of the lambda terms.
Lambda terms: The following three rules provide an inductive definition.
- A variable, $x$, is itself a valid lambda term.
- If $t$ is a lambda term, and $x$ is a variable, then $( \lambda x, t )$ is a lambda term (called an abstraction).
- If $t$ and $s$ are lambda terms, then $(ts)$ is a lambda term (called an application). Nothing else is a lambda term. Thus, a lambda term is valid if and only if it can be obtained by repeated application of these three rules.
I will look at the mathematical background in detail at some other time. Let’s move on to Python.
Understanding Lambda Functions in Python
Let’s consider the following simple code:
lambda x: x + 1
It can be divided in the following three parts:
- The keyword:
lambda
; indicates that a lambda function is being used. - A bound variable: x; refers to the variable to which we are applying the lambda function.
- A body: x+1; indicates what the lambda function does to the bounded variable.
Continuing with Go 101
Introduction to Source Code Elements
https://go101.org/article/basic-code-elements-introduction.html
command | package |
---|---|
any executable program | importable semantic unit of functionality |
According to the Church–Turing thesis, computable functions are exactly the functions that can be calculated using a mechanical calculation device given unlimited amounts of time and storage space. Equivalently, this thesis states that a function is computable if and only if it has an algorithm. Note that an algorithm in this sense is a sequence of steps a person with unlimited time and an unlimited supply of pen and paper could follow. From Wikipedia. ↩︎