JavaScript function calls and tail recursion tail

  • What is the function of tail calls and tail recursion
  • Last application function calls and tail recursion

 First, what is the tail and the tail recursive function calls

The final step is to call the end of the function refers to a function that calls another function.

1  // function a tail call Example 
2  function foo (X) {
 . 3      return G (X);
 . 4  }
 . 5  // call the function example of the last second 
. 6  function Fun (X) {
 . 7      IF (X> 0 ) {
 . 8          return G (X);
 . 9      }
 10      return Y (X);
 . 11 }

Call the difference between the last step and the last line of code, the code is not necessarily the final step in the last line, such as the second example. Here there is this kind of function can not be called a tail call:

1  // following the end of the function call is not such a case is called the 
2  function FU (X) {
 . 3      var Y = 10 * X;
 . 4      G (Y);
 . 5 }

Why is this not a function called tail call it? The reason is simple, because the last step is to perform the function return instruction, the instruction has two functions, the first function is a function to end the current execution, the second function is to transfer data back out instruction (function return values). And this return command will be executed with or without a declaration, returned without a statement of case data is undefined, so the above code is actually the following structure:

1 function fu(x){
2     var y = 10 * x;
3     g(y);
4     return undefined;
5 }

return instruction function is to shut down, and then return data. Here, it will lead to a problem, if not the last step function tail call happens? return instruction is followed by the following case, what would happen?

1  // factorial of the number 
2  function factorial (n-) {
 . 3      IF (n-1 || n-=== === 0) return 1 ;
 . 4      return n-* factorial (n-- 1 );
 . 5 }

Factorial algorithm example above this number can not be called the end of the function call, because the last step is the product of calculation, not purely a function call.

 Second, the application calls the function tail and tail-recursive

Tail call return instructions on a step function that is essentially the last executed, this part of the return data is a function execution. This seemingly simple instructions and its straightforward features and nothing special. However, when the function is executed, it will form a "call records" in memory, commonly referred to as "call frame." Note that when the function is called internally executed, that is a function called before the return instruction triggered because the function call after the return instruction will generate a separate function call stack, instead of adding the original function call on the call stack frame.

We allocate memory until the browser space is a limited resource, that is the call stack memory function is limited, if the function appears large loop nest function calls, each nested function calls will call the original function Add on top of the stack call frame, like the factorial of the number above if the incoming parameter is 100, then it will have 99 call frame on the call stack factorial function, if the argument and then a big point then? 1000 or more, this infinite stack may certainly bring a risk that stack overflow.

Look at the following example:

. 1  function FB (n-) {
 2      IF (n-n-||. 1 == == 2 ) {
 . 3          return . 1
 . 4      }
 . 5      return FB (n--. 1) FB + (n-- 2 );
 . 6  }
 . 7 the console.log ( fb (100)); // stack overflow, browser crashes

The above example (Fibonacci number) has the same algorithm with multiply mediated problem is that after the return instruction in the face of all the results of the calculation function, and this actually occurs is calculated on the current function, but also in function increasing the call stack call frame, until compliance with the export program logic will stop. But when the calculated value reaches a certain level will cause a stack overflow, causing the browser to Ben collapse.

Having said that, there has been no clear resolve what is tail-recursive, in fact, nothing can be resolved, it is to perform the function calls itself after the return instruction. Here then is the use of default parameters tail recursion and ES solution call frame factorial and Fibonacci algorithm column overflow problem:

1  // Use default ES6 + tail recursive algorithm implemented factorial 
2  function factorial1 (n-, Total = 1 ) {
 . 3      IF (n-1 || n-=== === 0) return Total;
 . 4      n-+ = 1 ;
 . 5      return factorial (n--. 1, n-* Total);
 . 6  }
 . 7  // default value for ES6 + tail recursive implementation number of number Fibonacci column algorithm 
. 8  function FB1 (n-, ACl =. 1, AC2 =. 1 ) {
 . 9      IF (n-n-=== === 2 ||. 1) return AC2;
 10      return FB1 (n--. 1, AC2, ACl + AC2);
 . 11 }

In Ruan Yifeng teacher "ES6 standard entry-Third Edition" P127, the teacher found two algorithms are computationally less calculate a value, such as teacher factorial factorial of 5 is the result of 24, this result makes me a start puzzled, personal teacher idea is inferred according to the counting computer (starting from 0), which is the index parameter specifies factorial results using the index parameter specifies where the result set Calcd. I do not know this speculation is correct, if there is something wrong place also please correct me.

And I used in the example is the factorial result value, not factorial results table index.

 

Guess you like

Origin www.cnblogs.com/ZheOneAndOnly/p/11368056.html