I'm learning python using the tutorial on the official python website and came across this example:
>>> def make_incrementor(n):
... return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43
Where does x
get it's value from? I'm not familiar with how lambda works, I understand anonymous functions just fine from javascript but this has me stumped. Anyone care to shed some light? I'd be grateful.
x
gets its value from the parameter used to callf
.n
gets its value from the parameter used to callmake_incrementor
.make_incrementor is a function that returns a function:
When we call make_incrementor, we bind the parameter 42 to n, and return the concrete function:
This function we will return will therefore look like:
f is bound to the returned function, so f will conceptually look like:
When we call f, we provide the value for x
Here's how I understand lambda:
When we write x2, we often confuse two distinct ideas. Consider:
x2 has an odd number of factors when x is an integer.
x2 is larger than x when x>1.
x2 has derivative 2x.
x2 has an inverse, namely sqrt(x), for x>0.
The first two statements are about the square of a particular but unspecified number. In those statements "x" represents an arbitrary single thing, and x2 represents a single related thing.
The third and fourth statement are about x2, the function. But this is not clearly communicated in 3. and 4. except by context and shared understanding of Calculus. We need a notational device to distinguish between x2 the single (but arbitrary) value, and x2 the function. The device also needs to communicate that it is a function of x. Therefore lambda is invented:
"lambda x.x2" is written to make this distinction and communicate "the function of x whose value at x is x2". The lambda operator takes a name (x) and an expression (x2) and returns a function. This has the same consequences as normal function definition, except that the function doesn't automatically receive a name.
Disclaimer: I have pretty much no Python background. This is going off my Scheme/lambda calculus knowledge.
make_incrementor defines a function to make functions. make_incrementor(42) returns a function with x bounded to the lambda, and n with a value of 42. When you call the function f, the argument of f replaces the bound variable.
lambda
brings some lambda calculus to Python. In essence, this is what's happening: normally in lambda calculus a statement would look something like this:This would pass
3
and4
to theadd
function and store the result insum
. However, we could also write something along the lines ofadd 3
. Now, sinceadd
expects two arguments, we now get an object (or function, if you will) expecting one argument. That function will then calladd
with 3 as its first argument and whatever we pass it as the second argument. We can now do this:This will be equivalent to the previous example. However, you can now use
func
whenever you want to add 3 to something. (Granted, this doesn't seem useful in this example, but in more complex situations it is).All this is closely related to currying, something very central in most functional languages. If you're interested in lambda calculus and its similarity to regular mathematics, I highly recommend that you take a look at Haskell.
First off, here's a translation into JavaScript:
A lambda expression consists of three parts.
lambda
(In JavaScript, the wordfunction
)(...)
){...}
, automatically returned)The list of parameters is the part before the colon. These parameters are visible within the scope of the expression. Whatever the expression evaluates to is returned as the result of the function.
Unlike in JavaScript, a lambda can only contain a single expression; you can't make an anonymous multi-line function with lambda.