A new understanding of recursion

Source: Internet
Author: User

Today I saw a discussion on SICP on recursion. Understand a truth:

形式上是递归的不等于本质上也是递归的。

If a function is formally recursive, that is, he calls himself. Not equal to this function is recursive in nature, it is possible that this function is iterative in nature.

For a function that is essentially recursive, when the recursion depth increases, it is necessary to record all the states on the previous path, that is, the memory growth is also linear. For a function that is essentially an iteration, although formally recursive, this recursion is attributed to "tail recursion". This kind of function, when the recursion depth increases, essentially does not need the memory also grows linearly, but only then needs the fixed quantity memory record the current state to be OK. The realization of "tail recursion", in fact, can remove the language needed for , while and so on for the iterative syntax of sugar.

However, when implemented, some programming languages implement all recursion as a form of memory growing with the increase in recursion depth. But a language like Scheme is not going to happen this way. So we'll hear about it. In functional programming, the tail recursion is implemented in the form of iteration, and when the recursion depth increases, the stack is not exploded. The truth is right here.

Calculation n! used in SICP Two ways to explain the problem:

Method One:

 (define (factorial N) (if (= n 1) 1 (* N (Factorial (-N 1))))  
factorial (6) = 6 * factorial (5) = 6 * (5 * factorial (4)) = ... = 6 * (5 * (4 * (3 * (2 *) (factorial (1)))))) = 6 * (5 * (4 * (3 * (2 * (1)))) = 6 * (5 * (4 * (3 * 2)) = 6 * (5 * (4 * 6)) = 6 * (5 *) = 6 * 120= 720  

As you can see from the code and the example above, this method is recursive in nature, and it needs to record all the states from the top of the function to the bottom path, and then calculate the past in the opposite direction.

Method Two:

 (define (factorial n) (Fact-iter 1 1 N)) (Define (fact-iter product counter max-count) (if (> Counter max-count) Product (fact-iter (* counter product) (+ counter 1)))  
factorial(6)= fact-iter(1 1 6)= fact-iter(1 2 6)= fact-iter(2 3 6)= fact-iter(6 4 6)= fact-iter(24 5 6)= fact-iter(120 6 6)= fact-iter(720 7 6)

As you can see from the code above and the example above, this method is iterative in nature, it only needs to record the current state (product counter max-count) on the line, from the front to the back, until the counter is added to the max-count.

corresponding to our usual non-functional language, method one is actually implemented by recursion, method two is implemented with a for or while iteration, but in Scheme, the iteration is also in the form of recursive expression, but in the implementation of the interpreter, in essence, is an iterative process.

A new understanding of 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.