Higher order functions and recursion

What is a higher-order function?

  A variable can point to a function, the parameters of a function can receive variables, and a function can receive another function as a parameter, which we call higher-order functions.

def calc(x):
    return x*x
f = calc
f = lambda x:x*x   #anonymous function
variable points to function
x = 10
def calc(x):
    return x*x
function receives variable
def func(x,y):
    return x + y
def calc(x):
    return x
f = calc(func)
print(f(5,6))
A function receives another function as a parameter

As long as any of the following conditions are met, it is a higher-order function:

  1. Receive one or more functions as input (parameters).

  2, return returns another function.

recursion

  Recursion is when a function calls itself while it is running.

def recursion(n):
    print(n)
    recursion(n +1) #Each execution will call itself once, this function is equivalent to infinite loop 
recursion(1)
recursion

 The effect is that this function is constantly calling itself, and each call is n+1, which is equivalent to a loop.

In layman's terms, each function has not been launched when calling itself, occupying too much memory or causing a crash. The essence is because the function call is implemented through the data structure of the stack (stack). Each time a function call stack is entered, a stack frame will be added, and each time the function returns, a stack frame will be built on the stack. Since the size of the stack is not infinite, too many recursive calls will cause stack overflow.

The stack structure of the function call:

 Features of recursion

Divide 10 by 2 until it reaches 0. (How many times you call, how many times to end)

# def calc(n): 
#      val = n//2 
#      print(val) 
#      if val ==0: 
#          return 1 
#      calc(val) 
#      print(val) #Return one by one in reverse order 
# calc(10 )

def calc(n):
    print(n)
    if n //2 >0:
        calc(n//2)
    print(n)
calc(10)
10 divided by 2 recursively

 Find the factorial

Representation of the factorial of any natural number n greater than 1: 
n!=1*2*3*4*5*6*...*n or n!=n*(n-1)!
For example: 4!=4* 3*2*1=24
def factorial(n):
    if n ==1:
        return 1
    return n*factorial(n-1)
print(factorial(5))

===> factorial(5)
===> 5 * factorial(4)
===> 5 * (4 * factorial(3))
===> 5 * (4 * (3 * factorial(2)))
===> 5 * (4 * (3 * (2 * factorial(1))))
===> 5 * (4 * (3 * (2 * 1)))
===> 5 * (4 * (3 * 2))
===> 5 * (4 * 6)
===> 5 * 24
===> 120
class

Recursion: a, call itself by itself. b, there are end conditions, both of which must be satisfied

Tail recursion means that when the function returns, it calls itself, and the return statement cannot contain an expression. In this way, the compiler or interpreter can optimize the tail recursion, so that no matter how many times the recursion itself is called, it only occupies one stack frame, and there will be no stack overflow.

def fact(n):
    return fact_iter(n, 1)

def fact_iter(num, product):
    if num == 1:
        return product
    return fact_iter(num - 1, num * product)
print(fact(5))
print(fact_iter(5,1))

As you can see, return fact_iter(num - 1, num * product)only the recursive function itself is returned, num - 1and num * productit will be calculated before the function call, without affecting the function call.

fact(5)The corresponding fact_iter(5, 1)calls are as follows:

===> fact_iter(5, 1)
===> fact_iter(4, 5)
===> fact_iter(3, 20) ===> fact_iter(2, 60) ===> fact_iter(1, 120) ===> 120
尾递归调用时,如果做了优化,栈不会增长,因此,无论多少次调用也不会导致栈溢出。

summary

The advantage of using recursive functions is that the logic is simple and clear, and the disadvantage is that too deep calls can lead to stack overflow.

Languages ​​optimized for tail recursion can prevent stack overflows by tail recursion. Tail recursion is actually equivalent to looping. Programming languages ​​without looping statements can only implement looping through tail recursion.

The standard Python interpreter is not optimized for tail recursion, and any recursive function has stack overflow problems.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325388282&siteId=291194637