A self-reference object in Python for recursive calls in lambda expressions
baruchel.github.io
baruchel.github.io
def fix(g):
return (lambda f: g(lambda *args, **kwargs: f(f)(*args, **kwargs)))(
lambda f: g(lambda *args, **kwargs: f(f)(*args, **kwargs)))
Then: fac = fix(lambda self: lambda n: 1 if n <= 1 else n*self(n-1))
For the second example: fac2 = fix(lambda self: lambda n: 1 if n <= 1 else n*((lambda k: self(k-1))(n)))I have a more serious module implementing tail-calls (either for recursion or not) where the end-user has to follow the syntax of the Y-combinator: see https://news.ycombinator.com/item?id=9901309
Can anybody suggest advanced Python books to dig out more in such matters?
Reading the official python documentation as though it were a book can lead to a lot of insights: