Question about final recursive call

After participating in the discussion on fiasco on this issue , I would like to ask a question to the entire community.

In what scenarios will tail optimization be applied to the .Net code?

Please support your answers with reliable, up-to-date sources or reproducible experiments.

+3
source share
1 answer

According to "Expert F #," written by don Sim and others, F # does tail call optimization. I seem to remember reading Eric Lippert's blog that the C # compiler (any version) does not. Correct me if I am wrong, Eric. In all cases, tail call optimizations can be performed when the last command must invoke a method. This will often be a recursive call to the method itself, but not required. Optimization can be performed because it is guaranteed that the current stack stack is no longer needed. However, if after the operation is simply performed, the optimization cannot be performed.

int Fib(int n)
{
  if(n < 2)
     return 1;

  return Fib(n-1) + Fib(n-2);
}

, + , Fib. ( , , , Expert F #, .)

int Fib(int n, int a, int b)
{
  if(n == 0)
    return a+b;
  return Fib(n-1,a+b,a);
}

, Fib, , .

+4

Source: https://habr.com/ru/post/1733458/


All Articles