Tan Yong Boon
Tan Yong Boon

Reputation: 117

How to evaluate the following lambda expression?

The following is a question I have on lambda expressions:

def combinator(y):
    return (lambda x: lambda y: x(y))(lambda x:y)

Evaluate the following expression combinator(lambda x: x*10)(11)(12).

I understand that return statements return a value so perhaps, we should start by simplifying the lambda expression of the return statement:

(lambda x: lambda y: x(y))(lambda x:y) simplifies to (lambda y: (lambda x:y)(y)),

which further simplifies to (lambda y: y)

However, following the above logic would lead me to the wrong answer. The answer is 120.

I would like to ask, what is the method to deal with the above lambda expression?

Upvotes: 0

Views: 685

Answers (1)

outis
outis

Reputation: 77400

(lambda x: lambda y: x(y))(lambda x:y) simplifies to (lambda y: (lambda x:y)(y)),

No. y is free in (lambda x:y), so applying (lambda x: lambda y: x(y)) by simply substituting the expression for x results in variable capture. If you are going to simplify the expression, you must first rewrite (lambda x: lambda y: x(y)) to an alpha-equivalent expression that has no bound variables with the same names as free variables of (lambda x:y) to avoid the capture. How to do this is left as an exercise.

Upvotes: 1

Related Questions