def compose(f, g):
return lambda x:f(g(x))
def thrice(f):
return compose(f, compose(f, f))
def repeated(f, n):
if n == 0:
return identity
else:
return compose(f, repeated(f, n - 1))
def sq(x): return x**2
1) print(thrice(thrice)(sq)(1))
2) print(thrice(thrice(sq)(2))
Can anyone explain to me why the first function returns 1 and the second function does not work? I was thinking thrice(thrice)(sq) will give me sq∘sq∘..... 27 times, so sq(1) is 1^27=1, is that correct? thanks.
repeatedis never used, why include it into the question?functional programminglooks like in Python ;).