def compose(f, g):
return lambda x:f(g(x))
def thrice(f):
return compose(f, compose(f, f))
add1 = lambda x: x + 1
print(thrice(thrice)(add1)(6)) = 33
谁能向我解释为什么这是33?
我应该开始阅读复合函数的哪一边?
最佳答案
1)关于数学,compose(f, g) = f ∘ g
2)然后thrice(f) = f ∘ f ∘ f
。
3)然后T := thrice(thrice) = thrice ∘ thrice ∘ thrice
4)然后T(f) = f ∘ f ∘ f ∘ f ∘ f ∘ ...
#27次
5)然后thrice(thrice)(add1) = T(add1) = add1 ∘ add1 ∘ ...
#27次
6)然后thrice(thrice(add1))(x) = x + 27