我首先将这些论点分开:
lambda x: summation(lambda x: x, 1, lambda x: x + 1, x)
将这些变量替换回原始函数并简化它:
def inner_function(x):
soma = 0
linf = 1
while linf <= x:
soma += linf + 1
linf += 1
return soma
再简化一点:
def inner_function(x):
soma = 0
for linf in range(1, x + 1):
soma += linf
return soma
还有一点:
inner_function = lambda x: sum(range(1, x + 1))
还有更多:
inner_function = lambda x: x * (x + 1) / 2
现在您的原始功能变为:
def summation(calc_termo, linf, prox, lsup):
soma = 0
while linf <= lsup:
soma = soma + calc_termo(linf)
linf = prox(linf)
return soma
summation(inner_function, 1, lambda x: x + 1, 5)
或者:
def summation(linf, prox, lsup):
soma = 0
while linf <= lsup:
soma = soma + linf * (linf + 1) / 2
linf = prox(linf)
return soma
summation(1, lambda x: x + 1, 5)
你可以从那里拿走它。我有:
summation = lambda: sum(n * (n + 1) / 2 for n in range(6))
这等于:
sum(sum(range(n + 1)) for n in range(6))