On the interpretation of the tail recursion

Source: Internet
Author: User

Come to know

http://www.zhihu.com/question/20761771/answer/19996299


The tail recursion and the general recursion are different in the memory occupy, the common recursive creates the stack accumulates and then calculates the contraction, tail recursion only consumes constant memory.(As with iterations). A memory footprint curve is described in SICP, which is used as an example of the Python code in the answer ( General recursion):
def recsum (x):   if x = =1    :return  x  Else:    return x + recsum (x-1)

When calling Recsum (5), the following condition occurs in the Python debugger:

Recsum (5)5 + recsum (4)5 + (4 + recsum (3))5 + (4 + (3 + recsum (2)))5 + (4 + (3 + (2 + R) Ecsum (1))) 5 + (4 + (3 + (2 + 1)))5 + (4 + (3 + 3))5 + (4 + 6)5 + 1015

This curve represents the peak of memory footprint, from left to right, up to the top, and then from right to left. And we don't usually want this to happen, so using iterations, we only occupy the constant stack space (update this stack!). Rather than extend him).

---------------------
(An alternative: Iteration
 for  in range (6):  + = i

Because python,java,pascal can not achieve tail recursion optimization in language (Tail call optimization, TCO), so the use of a for, while, Goto and other special structures instead of recursive representations. Scheme does not need to be such a tortuous expression, once written in the form of tail recursion, you can carry out tail recursion optimization.

---------------------
Python can be written in ( Tail recursion):
def tailrecsum (x, running_total=0):  if x = = 0    :return  Running_total  Else:    return tailrecsum (x-1, running_total + x)

Theoretically similar to the above:

Tailrecsum (5, 0) tailrecsum (4, 5) Tailrecsum (3, 9) Tailrecsum (2,) tailrecsum (1, 15)

It is observed that the values of the actual variables of the form variable y in tailrecsum (x, y) are constantly updated, and it is clear that the latter is the same as the Y value of each recsum () call, and only deepens at the level. So, the tail recursion is a variable that passes the parameters of the change to the recursive function .

How to write tail recursion? Formally as long as the last return statement is a simple function can be. Such as:
return tailrec(x+1);
and
return tailrec(x+1) + x;
It is not possible. Because the actual variable inside the Tailrec () function cannot be updated, just create a new stack.

(turn) explanation of the tail recursion

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.