Python recursion
If a function calls the function itself internally, the function is a recursive function;
Here's a classic example: using Python recursion to find factorial
def fact (j): sum=0 if j==0: sum=1 Else: sum =j*fact (j-1) return sum for in range (5): Print ('%d!=%d'% (i,fact (i)))
After the CPS transformation eliminates the general recursion,
id=Lambda x:xdef Factcps (n): def F (n,k): if n==0: return K (1) Else: return f (n-1,Lambda x:k (nx)) return F (n,id)
Tail recursion Optimization
Tail recursion is based on the tail call of the function, each level calls the return value of the return function to update the call stack, instead of creating a new call stack, similar to the implementation of the iteration, time and space optimization of the general recursion;
def fact (n,sum=0) if n==0: return sum Else : return fact (n-1,sum+n)
"Life is short, Python is a song."--python Functional Programming 03