Lambda expressions are the expressions evaluate to functions.

square = lambda x : x * x

lambda x : x * x is an expression evaluates to a function with parameter formal x and returns x * x as value.
We can also use lambda expression as a call expression, too

(lambda x : x * x)(3) # 9

The return must be a single expression, as the body of lambda expression.

It can also have more than two arguments or no argument. Lambdas can return other lambdas. They can have functions as arguments as well.

An Interesting Way to Find Inverse Function

def search(f):
	x = 0
	while not f(x):
		x += 1
	return x
 
def inverse(f):
	return lambda y : search((lambda x : f(x) == y ))

Another Interesting Example of Defining a Recursive Function

def make_anonymous_factorial():
    return (lambda f: lambda k: f(f, k))(
        lambda f, k: 1 if k == 1 else mul(k, f(f, sub(k, 1)))
    )

THAT IS REALLY ABSTRACT!!!
Let’s figure out what happened!

  1. The lambda front created a function which receive a function as an argument f and return another function.

Yes, it is a higher-order function.

  1. The returned function takes one argument k, then return f(f, k).

It shows that the function f, the outer lambda’s argument, is self-referred.

  1. So we define lambda for third time: a function which takes two argument,f and k. In this case, f is a function and k is a variable to calculate factorial.

OK, let’s shorten “the third lambda” as f. And yes, every f is exactly this function.

  1. f is the argument for the first lambda.

So no the third lambda is self-referred.

  1. So the only one argument should input is k, for the second lambda.
  2. When k input, we will call f(f, k).

Do you still remember f has TWO arguments, one is f and one is k? That why f has two arguments, because so that it can call itself in it.

Let’s convert it to a more easy version.

fact = lambda f, k : 1 if k == 1 else k * f(f, k - 1)
self_refer = lambda f : lambda k : f(f, k)
tfwra = self_refer(fact) # This is the return of first lambda
# Abbreviation for the_function_we_return_above
"""tfwar == fact(fact, )"""
tfwar(5)
=="""fact(fact, 5)"""
==fact, 5 : 1 if k == 1 else k * fact(fact, k - 1)

Without the first lambda, we can’t take f as the argument for f itself, that the purpose to define it.