Anonymous recursion: Difference between revisions

(Use the Aldor compiler as the primary solution.)
Line 1,440:
>>> [ Y(fib)(i) for i in range(-2, 10) ]
[None, None, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34]</lang>
 
An interesting approach using introspection (from http://metapython.blogspot.com/2010/11/recursive-lambda-functions.html)
<lang python>
>>> from inspect import currentframe
>>> from types import FunctionType
>>> def myself (*args, **kw):
... caller_frame = currentframe(1)
... code = caller_frame.f_code
... return FunctionType(code, caller_frame.f_globals)(*args, **kw)
...
>>> print "factorial(5) =",
>>> print (lambda n:1 if n<=1 else n*myself(n-1)) ( 5 )
</lang>
 
=={{header|Qi}}==