According to the OS response in English:
https://stackoverflow.com/questions/481692/can-a-lambda-function-call-itself-recursively-in-python
It is possible to assemble a logical sum (or other algorithms) with recursiveness using functions lambda
in Python.
An important point about recursive functions is that, obligatorily, they must have a stop condition, that is, at some point, the algorithm must return to avoid an infinite looping (references at the end of the answer).
According to the answer above, there are several ways to create these expressions.
The examples below (recursively) perform the calculation:
x=10
soma = 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 55
Examples:
a) Name the function lambda
created (easier):
soma = lambda x: 1 if x == 1 else x+soma(x-1)
Upshot:
>>> soma = lambda x: 1 if x == 1 else x+soma(x-1)
>>> soma(10)
55
The variable soma
receives the value of an anonymous function (lambda
)
and returns if the function parameter is 1
(stop condition).
If the value is greater than 1
, it adds the value of the parameter x
the result of a recursive call with parameter x-1
,
through the reference stored in the variable soma
.
b) Using an auxiliary function:
def soma(f, *p, **kw):
return f(f, *p, **kw)
soma( (lambda fr, x: 1 if x == 1 else x + fr(fr, x-1)), 10 )
Upshot:
>>> soma( (lambda fr, x: 1 if x == 1 else x + fr(fr, x-1)), 10 )
55
This type of function is known as Fixed point combinator (see references) or Y-Kombinator de Lemmy.
The auxiliary function soma
receives 3 parameters:
f
- will receive an anonymous function
*p
- receives a list of positional parameters
**kw
- receives a list of named parameters
consult: Keyword Arguments - Python Documentation (in English)
When calling the function soma
, the first parameter reported is a function lambda
which receives 2 parameters: the function itself (in fr
) and the value 10
.
The recursive call occurs in: x + fr(fr, x-1)
and subsequently on return f(f, *p, **kw)
.
The stopping condition is the same as the previous one.
c) Only with anonymous functions (more complex):
(lambda f1: lambda v1: f1(f1, v1))(lambda f, x: 1 if x == 1 else x+f(f, x-1))(10)
Upshot:
>>> (lambda f1: lambda v1: f1(f1, v1))(lambda f, x: 1 if x == 1 else x+f(f, x-1))(10)
55
This function is also a variation of the Y-Kombinator de Lemmy.
Dividing the command line into 3 parts:
1) The first part is equivalent to the auxiliary function of the example (b) above.
The parameter f1
receives the function of item 2 (below) and the parameter v1
will receive the value 10
, from the recursive call f1(f1, v1)
:
(lambda f1: lambda v1: f1(f1, v1))
2) The second part is the function that performs the calculation:
(lambda f, x: 1 if x == 1 else x+f(f, x-1))
3) The third part is value 10, which will be sent to the parameter v1
, and, subsequently to x
:
(10)
To facilitate understanding of this example, the command is equivalent to the example (to) as follows:
f1 = lambda f, x: 1 if x == 1 else x+f(f, x-1)
v1 = 10
f1(f1, v1)
For the tests, it was used Python 2.7.11.
Important: as the references, this type of code has academic utility and probably the use in production environments is not recommended.
References:
Wikilivros - Algorithms and Data Structures/Recursiveness
Wikipedia - Recursiveness - computer science
Wikipedia - Lambda calculus
Wikipedia - Fixed point combinator
Welcome to Stackoverflow. Friend I recommend that you read the Stack Overflow Tour, will help you to be clearer in your questions. http://answall.com/tour
– Danilo Pádua
Your question is about python or lambda-Calculus?
– Victor Stafusa
It doesn’t matter, because it is possible to do it in Python.
– user36177
Did you have any error code? Or the result was not as expected?
– Math
No, because I didn’t get to implement I’m wanting to assemble the logic.
– user36177
Writing recursive lambda functions is a bit difficult, since functions have no name. You will need to use a fixed point operator or change your number definition (if you use Church numerals instead of Python integers it is easier to sum)
– hugomg