I am trying to get a list of functions defined recursively by storing each step of a math serie:
x^0+x^1+x^2+x^3+... → f0(x)=1, f1(x)=1+x, f2(x)=1+x+x², f3(x)=1+x+x²+x³, ...
I coded this in python:
n = 3
devs = [lambda x: 1]
for k in range(n):
devs.append(lambda x, f=devs[k]: f(x) + x**(k+1))
print(devs[-1](4)) # x=4
I learned in this answer that you need to pass the previous function as default argument in order to solve the maximum recursion depth error, but this outputs 193.
I think it has to do with the k in the default parameter. It seems to calculate: 1+4^3+4^3+4^3=193, or more accurately 1+n*x^n instead of x^0+x^1+...+x^n
Can you please tell me what am I doing wrong here?