I want to create list of lambdas by a given size n (so manual assignment won't work) where each element depends on previous function and it index in the list.
I've tried various stuff like this :
n = 3
func_list = [lambda x : 1]
for k in range(1,n):
func_list.append(lambda x : func_list[k-1](x) * (x+k))
print(list(map(lambda f : print(f(1)), func_list)))
But in all attempts I've got error message about recursion depth:
RecursionError Traceback (most recent call last)
<ipython-input-296-f35123a830c4> in <module>
2 for k in range(1,3):
3 func_list.append(lambda x : func_list[k-1](x) * (x+k))
----> 4 print(list(map(lambda f : print(f(1)), func_list)))
<ipython-input-296-f35123a830c4> in <lambda>(f)
2 for k in range(1,3):
3 func_list.append(lambda x : func_list[k-1](x) * (x+k))
----> 4 print(list(map(lambda f : print(f(1)), func_list)))
<ipython-input-296-f35123a830c4> in <lambda>(x)
1 func_list = [lambda x : 1]
2 for k in range(1,3):
----> 3 func_list.append(lambda x : func_list[k-1](x) * (x+k))
4 print(list(map(lambda f : print(f(1)), func_list)))
... last 1 frames repeated, from the frame below ...
<ipython-input-296-f35123a830c4> in <lambda>(x)
1 func_list = [lambda x : 1]
2 for k in range(1,3):
----> 3 func_list.append(lambda x : func_list[k-1](x) * (x+k))
4 print(list(map(lambda f : print(f(1)), func_list)))
RecursionError: maximum recursion depth exceeded
func_list[k-1](x)late bindingof closures (lambdas): e.g. read thisreference K, andk == 2, so everyk - 1will return the same result in all method so you keep returning the same element until you got the error