11

I have just started learning python. I came across lambda functions. On one of the problems, the author asked to write a one liner lambda function for factorial of a number.

This is the solution that was 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)

I cannot understand the weird syntax. What does a(a,b) mean?

Can someone explain?

Thanks

3
  • 13
    I got a better one: lambda b: math.factorial(b) Commented Mar 14, 2013 at 4:50
  • 2
    Dont reinvent the square-shaped wheel. Commented Mar 14, 2013 at 4:53
  • Even better: lambda b: __import__('math').factorial(b) Commented Aug 11, 2024 at 23:33

11 Answers 11

11

The factorial itself is almost as you'd expect it. You infer that the a is... the factorial function. b is the actual parameter.

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

This bit is the application of the factorial:

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

a is the factorial function itself. It takes itself as its first argument, and the evaluation point as the second. This can be generalized to recursive_lambda as long as you don't mind a(a, b - 1) instead of 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 we have the outer part:

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

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


If you actually wanted to have a recursive lambda, you could just name the lambda:

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

If not, you can use a simple helper function. You'll notice that ret is a lambda that can refer to itself, unlike in the previous 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

Both ways you don't have to resort to ridiculous means of passing the lambda to itself.

Sign up to request clarification or add additional context in comments.

3 Comments

To clarify: a is being passed to itself?
That is a whole new level of twisted.
Sorry, for replying after so long. I understood what you explained. I almost forgot that I didn't post back.
7

It is this simple:

n=input()

print reduce(lambda x,y:x*y,range(1,n+1))

Comments

6

Let's peel this one liner open like an onion.

print (lambda b: (Y))(num)

We are making an anonymous function (the keyword lambda means we're about to type a series of parameter names, then a colon, then a function that uses those parameters) and then pass it num to satisfy its one parameter.

   (lambda a, b: a(a, b))(X,b)

Inside of the lambda, we define another lambda. Call this lambda Y. This one takes two parameters, a and b. a is called with a and b, so a is a callable that takes itself and one other parameter

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

These are the parameters to Y. The first one is a lambda function, call it X. We can see that X is the factorial function, and that the second parameter will become its number.

That is, if we go up and look at Y, we can see that we will call:

X(X, b)

which will do

b*X(X, b-1) if b > 0 else 1

and call itself, forming the recursive part of factorial.

And looking all the way back outside, we can see that b is num that we passed into the outermost lambda.

num*X(X, b-1) if num > 0 else 1

This is kind of confusing because it was written as a confusing one liner :)

1 Comment

It was an online competition on one liners. And I was preparing for that. It was confusing but after spending a great amount of time, I think I got it down. Thanks
2

There are two hard parts about this function.
1. lambda a, b: b*a(a, b-1) if b > 0 else 1.
2. the "b" that's folowing 1.

For 1, it's nothing more than:

def f(a, b):
    if b > 0:
        b * a(a, b - 1)
    else:
        1

For 2, this b

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

is actually this b:

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

The reason is that it's not inside the definition of the second and third lambda, so it refers to the first b.

After we apply num and strip off the outer function:

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

It's just applying a function to a tuple, (lambda a, b: b*a(a, b-1) if b > 0 else 1, num)
Let's call this tuple as (f, num) (f's def is above) Applying lambda a, b: a(a, b) on it, we get

f(f, num).

Suppose your num is 5.
By definiton of f, it first evaluates to

5 * f(f, 4)  

Then to:

5 * (4 * f(f, 3)) 

All the way down to

5 * (4 * (3 * (2 * (1 * f(f, 0)))))

f(f, 0) goes to 1.

5 * (4 * (3 * (2 * (1 * 1))))

Here we go, the factorial of 5.

Comments

2

We can use the below lambda expression

     fact = lambda n:1 if n==0 else n*fact(n-1)
     print(fact(5)
     >>> 120

Comments

0

the generalized def of recursive lambda is as below

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

Comments

0

Very nicely explained! Only one minor fix: if b > 1 NOT if b > 0

The result is the same but logically more correct as it is executing one unnecessary additional loop (even though multiplying by 1)

Wikipedia => n!, is the product of all positive integers less than or equal to n

Comments

0

if you want to do a factorial using lambda expression then you need to put if else condition like:

if x==1: 
    return 1 
else 
    return x*function_name(x-1)

for example fact is my lambda expression:

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

this is recursive if else condition

Comments

0

Easy Method to find Factorial using Lambda Function

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

print(fac(5))

output: 120

User Input to find Factorial

def fac():

    fac_num = int(input("Enter Number to find Factorial "))

    factorial = lambda f : f * factorial(f-1) if f > 0 else 1

    print(factorial(fac_num))

fac()

Enter Number to find Factorial : 5

120

Comments

0

The code in question:

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

To make it clearer, let me replace variable names a with f, and b with n.

lambda n : (lambda f, n : f(f, n)) (lambda f, n : n * f(f, n-1) if n > 0 else 1,  n)

Recursive Factorial is a function that will call itself, or be applied to itself, something like f(f). Let us set Factorial(n) to be f(f,n) and compute as follows:

def func(f, n):  # takes a function and a number, return a number.
        if n > 0 :
            return n * f(f, n-1)
        else :
            return 1

Translating the above def func into lambda notation:

func is     lambda f, n : n * f(f, n-1) if n > 0 else 1 

func is then to be applied to itself along with integer n.

Back to beginning statement, Factorial(n) is f(f,n), or f(func,n) when func is applied into f.

def Factorial(n):  
    # return f (func, n)  # next, substitute f and func with their corresponding lambdas.
    return (lambda f, n : f(f, n))  (lambda f, n : n * f(f, n-1) if n > 0 else 1, n)

and using lambda instead of def Factorial, the whole thing becomes:

lambda n : (lambda f, n : f(f, n)) (lambda f, n : n * f(f, n-1) if n > 0 else 1, n)

Comments

-1
while True: 
    #It is this simple:
    from functools import reduce
    n=input('>>')
    n=int(n)
    if n==0:
        print('factorial: ',1)
    elif n<0:
        print('invalid input')
    else:
        print('factorial: ',(reduce(lambda x,y:x*y,list(range(1,n+1)))))

2 Comments

The question was about understanding a complex expression, not about coming up with a new one. Also, you are using an imported function reduce, which was,implicitly, not allowed.
Another thing - an import statement inside a loop - definitely a bad idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.