Examples of Python advanced tail recursion usage, python advanced

Source: Internet
Author: User

Examples of Python advanced tail recursion usage, python advanced

The author is a snake friend who is addicted to the inability to extricate himself from Python. To improve his skills, he will publish Python's key and interesting examples to a short book.

Tail recursion

If all recursive calls to a function appear at the end of the function, we call this function tail recursion. When a recursive call is the last statement executed in the entire function body and its return value is not part of the expression, this recursive call is tail recursion. The feature of the tail recursive function is that you do not need to perform any operations in the regression process. This feature is very important because most modern compilers will use this feature to automatically generate optimized code.

(It comes from a degree where people do not speak)

The following is my personal understanding: it is the calculation method to store the calculated value inside the function (not just tail recursion, of course), so you do not need to create a new one in the stack, this greatly saves space. The final result returned by a function call is a simple recursive function call (or return result) or tail recursion.

Instance

The instance is the same as the previous article by the author. We recommend that you read Python-recursion.

1. factorial

Regular recursive factorial:

def factorial(n):   if n == 0:    return 1  return factorial(n - 1) * n

Let's take a look at the execution process:

Factorial (4)
Factorial (3) * 4
Factorial (2) * 3*4
Factorial (1) * 2*3*4
Factorial (0) * 1*2*3*4
1*1*2*3*4
1*2*3*4
2*3*4
6*4
24

However, if you write the above functions as follows:

def factorial(n, acc=1):   if n == 0:    return acc  return factorial(n - 1, n * acc)

Let's look at the execution process again:

Factorial (4, 1)
Factorial (3, 4)
Factorial (2, 12)
Factorial (1, 24)
Factorial (0, 24)
24

Intuitively, we can see that this factorial function does not generate a series of gradually increasing intermediate variables during recursive calls, but stores the state in the acc variable. This form of recursion is called tail recursion.

2. Fibonacci Series

Regular delivery of Fibonacci series:

def fib(n):  if n < 2:    return n  else:    return fib(n - 1) + fib(n - 2)

Tail recursion:

def fib_tail(n, r, t):  if n == 1:    return r  else:    return fib_tail(n - 1, t, r + t)

All of a sudden it's full of pressure, but it's still a lot more efficient. Why not!

Summary

It can be seen that a temporary variable is generated during each recursive call, which increases the memory usage of the process. In this way, when executing code with a deep Number of recursive layers, in addition to unnecessary memory waste, it may also cause the famous stack overflow error.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.