Repeat a function composition n times in Python like Haskell's repeat
问题 This code does NOT work: def inc(x): return x + 1 def repeat(f, n): if n == 0: return lambda x: x else: return f( repeat(f, n - 1 ) ) inc_10 = repeat(inc, 10) #TypeError: unsupported operand type(s) for +: 'function' and 'int' ''' # Ideally print(inc_10(0)) # 10 ''' How can I write it in a more Pythonic way or in lambda calculus way ? 回答1: You still need to return a function, not the result of calling f , in the recursive case. # repeat :: (a -> a) -> Integer -> a -> a # repeat _ 0 = id #