recursive invocation of C # functional programming

Source: Internet
Author: User

About recursion believe that you are already familiar with the familiar, so I do not have to pay a lot of words here, do not understand the readers can find in the blog Park Many of the relevant blog. Here we go straight to the point, beginning to introduce the tail recursion.

Tail recursion

Normal recursion and tail recursion if only from the point of view of the code, we may not find his characteristics, so I use two stacks on the graph to show the specific gap, first of all, we look at the general recursive invocation of the situation, as shown in 1.1:

Assuming that the function executed here is FUNC1, and the Func1 in the recursive call itself, then we can see the stack on each call to FUNC1 will re-function return address and other parameters put on the stack, in the case of fewer recursion, so there is no problem. However, if the number of recursive calls reaches a certain order of magnitude, the stack space is consumed by the light. Therefore, the tail recursion is proposed. and the tail recursive stack diagram is shown in 1.2:

same or recursive, but each time you execute itself does not request new space in the stack space, similar to the effect of the For loop, in the face of a lot of recursion, there will be no problem. But the new problem comes out, and in C # The compiler does not optimize this step, but only when the JIT compiler executes. And only 64 bits are optimized. at the language level we also have to obey certain principles, in order to let the compiler know to optimize. Of course, some people who like to read the blog may have known that the tail recursion is to call themselves at the end of the last return. We can look at the tail call by a bunch of schematic codes:

int Func1 ()

{

return Func1 ();

}

Of course the above code will form a dead loop, because there are no baseline conditions. Let's give an example:

This function should probably be more familiar, that is, to calculate factorial. However, we can find that the function SUNFC the last return statement is not directly called the function itself, but X*sfunc (x-1), precisely because the previous x* will lead to the compiler can not be optimized, so that only the normal recursive invocation of the way to execute, then we need to use some patterns to change, First of all, we introduce the " accumulator transfer Mode ", perhaps the name comparison poised, in fact, is to pass the current calculation results to the next call function, so that when the baseline condition is reached directly based on the results of the last calculation of the final result of the return can be calculated, If this pattern is used in the code above, it will look like this:

With this pattern we turn back to the tail recursion and return the value of Y directly when the baseline condition is executed. There is no need to backtrack to the past. In addition to using this model, we can also take advantage of a " successor mode ", as well as the accumulator transfer mode also need to modify the function signature, add a parameter, we continue to modify the above code:

Compared to the accumulator transfer mode, this method is difficult to understand, in fact, sfunc when reaching the baseline condition Y is equivalent to the following lambda expression: a = a*4*3*2, and then call Y (1) to calculate the final result directly. In the simple point is Y this function is wrapped up several layers, such as the above function execution at the end of the call order of Y:

A is 1 passed to Y (2 * a) and the result is Y (2).

A is 2 passed to Y (3 * a) and the result is Y (6).

A is 6 passed to Y (4 * a) and the result is Y (24).

A is 24 passed to x = x, and the output is 24.

If still do not understand can only next breakpoint, debugging oneself pondering pondering, really do not understand can Q ask.

recursive invocation of C # functional programming

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.