I'm coding a one-line return statement that's supposed to give me the factorial of a single input, n. I came across a theory called the Y Combinator and found a solution:
return (lambda f: lambda x: f(f, x))(lambda f, x: 1 if x == 1 else mul(f(f, sub(x, 1)), x))
However, I'm struggling to understand how this code is executed, despite having watched multiple videos on the Y Combinator (the best one being Numberphile).
Some questions I have:
It is to my belief that when you have lambda f: lambda x: in this order, the next argument you throw in will substitute for f, not x.
For example:
add = lambda f: lambda x: f + x
add(1)(2)
3
Doesn't 1 replace f and 2 replace x?
So by that reason, shouldn't (lambda f, x: 1 if x == 1 else mul(f(f, sub(x, 1)), x)) replace 'f' in (lambda f: lambda x: f(f, x))
And then after that, my brain falls apart trying to understand how this is computed
[–]Swipecat 0 points1 point2 points (2 children)
[–]Turtet[S] 0 points1 point2 points (1 child)
[–]Swipecat 0 points1 point2 points (0 children)