Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I have recently begun learning python. I went over lambda functions. On one of the issues, the writer requested to compose a one liner lambda function for factorial of a number. 

Look at the solution which is given:

num = 5

print (lambda b: (lambda a, b: a(a, b))(lambda a, b: b*a(a, b-1) if b > 0 else 1,b))(num)

But, I can't understand this syntax. Can anyone please explain this?

Thanks

1 Answer

0 votes
by (26.4k points)

The actual factorial is nearly as you'd anticipate it. You gather that the a is... the factorial function. b is the real parameter. 

<factorial> = lambda a, b: b*a(a, b-1) if b > 0 else 1

This is actually a application of the factorial

<factorial-application> = (lambda a, b: a(a, b))(<factorial>, b)

Here, a is simply the factorial function. It accepts itself as its first argument, and the evaluation point will be the second. This can be summed up to recursive_lambda as long as you wouldn't fret a(a, b - 1) rather than a(b - 1):

recursive_lambda = (lambda func: lambda *args: func(func, *args))

print(recursive_lambda(lambda self, x: x * self(self, x - 1) if x > 0 else 1)(6))

# Or, using the function verbatim:

print(recursive_lambda(lambda a, b: b*a(a, b-1) if b > 0 else 1)(6))

So, the outer part:

(lambda b: <factorial-application>)(num)

As you can see all the caller needs to pass is the evaluation point. 

In the event that you really needed to have a recursive lambda, you could simply name the lambda: 

fact = lambda x: 1 if x == 0 else x * fact(x-1)

If not, you can utilize a basic helper function. You'll see that ret is a lambda that can r refer to itself, not at all like in the past code where no lambda could refer to itself. 

def recursive_lambda(func):

    def ret(*args):

        return func(ret, *args)

    return ret

print(recursive_lambda(lambda factorial, x: x * factorial(x - 1) if x > 1 else 1)(6))  # 720

The two different ways you don't need to turn to ridiculous methods for passing the lambda to itself.

Want to learn python to get expertise in the concepts of python? Join python certification course and get certified

Browse Categories

...