What is tail recursion?

前端 未结 28 3564
一个人的身影
一个人的身影 2020-11-21 05:03

Whilst starting to learn lisp, I\'ve come across the term tail-recursive. What does it mean exactly?

相关标签:
28条回答
  • 2020-11-21 05:21

    This is an excerpt from Structure and Interpretation of Computer Programs about tail recursion.

    In contrasting iteration and recursion, we must be careful not to confuse the notion of a recursive process with the notion of a recursive procedure. When we describe a procedure as recursive, we are referring to the syntactic fact that the procedure definition refers (either directly or indirectly) to the procedure itself. But when we describe a process as following a pattern that is, say, linearly recursive, we are speaking about how the process evolves, not about the syntax of how a procedure is written. It may seem disturbing that we refer to a recursive procedure such as fact-iter as generating an iterative process. However, the process really is iterative: Its state is captured completely by its three state variables, and an interpreter need keep track of only three variables in order to execute the process.

    One reason that the distinction between process and procedure may be confusing is that most implementations of common languages (including Ada, Pascal, and C) are designed in such a way that the interpretation of any recursive procedure consumes an amount of memory that grows with the number of procedure calls, even when the process described is, in principle, iterative. As a consequence, these languages can describe iterative processes only by resorting to special-purpose “looping constructs” such as do, repeat, until, for, and while. The implementation of Scheme does not share this defect. It will execute an iterative process in constant space, even if the iterative process is described by a recursive procedure. An implementation with this property is called tail-recursive. With a tail-recursive implementation, iteration can be expressed using the ordinary procedure call mechanism, so that special iteration constructs are useful only as syntactic sugar.

    0 讨论(0)
  • 2020-11-21 05:22

    It is a special form of recursion where the last operation of a function is a recursive call. The recursion may be optimized away by executing the call in the current stack frame and returning its result rather than creating a new stack frame.

    A recursive function is tail recursive when recursive call is the last thing executed by the function. For example the following C++ function print() is tail recursive.

    An example of tail recursive function

        void print(int n) 
    { 
    if (n < 0)  return; 
    cout << " " << n; 
    print(n-1);} 
    
    
    
    // The last executed statement is recursive call 
    

    The tail recursive functions considered better than non tail recursive functions as tail-recursion can be optimized by compiler. The idea used by compilers to optimize tail-recursive functions is simple, since the recursive call is the last statement, there is nothing left to do in the current function, so saving the current function’s stack frame is of no use.

    0 讨论(0)
  • 2020-11-21 05:23

    The jargon file has this to say about the definition of tail recursion:

    tail recursion /n./

    If you aren't sick of it already, see tail recursion.

    0 讨论(0)
  • 2020-11-21 05:23

    Tail recursion refers to the recursive call being last in the last logic instruction in the recursive algorithm.

    Typically in recursion, you have a base-case which is what stops the recursive calls and begins popping the call stack. To use a classic example, though more C-ish than Lisp, the factorial function illustrates tail recursion. The recursive call occurs after checking the base-case condition.

    factorial(x, fac=1) {
      if (x == 1)
         return fac;
       else
         return factorial(x-1, x*fac);
    }
    

    The initial call to factorial would be factorial(n) where fac=1 (default value) and n is the number for which the factorial is to be calculated.

    0 讨论(0)
  • 2020-11-21 05:23

    In Java, here's a possible tail recursive implementation of the Fibonacci function:

    public int tailRecursive(final int n) {
        if (n <= 2)
            return 1;
        return tailRecursiveAux(n, 1, 1);
    }
    
    private int tailRecursiveAux(int n, int iter, int acc) {
        if (iter == n)
            return acc;
        return tailRecursiveAux(n, ++iter, acc + iter);
    }
    

    Contrast this with the standard recursive implementation:

    public int recursive(final int n) {
        if (n <= 2)
            return 1;
        return recursive(n - 1) + recursive(n - 2);
    }
    
    0 讨论(0)
  • 2020-11-21 05:25

    This question has a lot of great answers... but I cannot help but chime in with an alternative take on how to define "tail recursion", or at least "proper tail recursion." Namely: should one look at it as a property of a particular expression in a program? Or should one look at it as a property of an implementation of a programming language?

    For more on the latter view, there is a classic paper by Will Clinger, "Proper Tail Recursion and Space Efficiency" (PLDI 1998), that defined "proper tail recursion" as a property of a programming language implementation. The definition is constructed to allow one to ignore implementation details (such as whether the call stack is actually represented via the runtime stack or via a heap-allocated linked list of frames).

    To accomplish this, it uses asymptotic analysis: not of program execution time as one usually sees, but rather of program space usage. This way, the space usage of a heap-allocated linked list vs a runtime call stack ends up being asymptotically equivalent; so one gets to ignore that programming language implementation detail (a detail which certainly matters quite a bit in practice, but can muddy the waters quite a bit when one attempts to determine whether a given implementation is satisfying the requirement to be "property tail recursive")

    The paper is worth careful study for a number of reasons:

    • It gives an inductive definition of the tail expressions and tail calls of a program. (Such a definition, and why such calls are important, seems to be the subject of most of the other answers given here.)

      Here are those definitions, just to provide a flavor of the text:

      Definition 1 The tail expressions of a program written in Core Scheme are defined inductively as follows.

      1. The body of a lambda expression is a tail expression
      2. If (if E0 E1 E2) is a tail expression, then both E1 and E2 are tail expressions.
      3. Nothing else is a tail expression.

      Definition 2 A tail call is a tail expression that is a procedure call.

    (a tail recursive call, or as the paper says, "self-tail call" is a special case of a tail call where the procedure is invoked itself.)

    • It provides formal definitions for six different "machines" for evaluating Core Scheme, where each machine has the same observable behavior except for the asymptotic space complexity class that each is in.

      For example, after giving definitions for machines with respectively, 1. stack-based memory management, 2. garbage collection but no tail calls, 3. garbage collection and tail calls, the paper continues onward with even more advanced storage management strategies, such as 4. "evlis tail recursion", where the environment does not need to be preserved across the evaluation of the last sub-expression argument in a tail call, 5. reducing the environment of a closure to just the free variables of that closure, and 6. so-called "safe-for-space" semantics as defined by Appel and Shao.

    • In order to prove that the machines actually belong to six distinct space complexity classes, the paper, for each pair of machines under comparison, provides concrete examples of programs that will expose asymptotic space blowup on one machine but not the other.


    (Reading over my answer now, I'm not sure if I'm managed to actually capture the crucial points of the Clinger paper. But, alas, I cannot devote more time to developing this answer right now.)

    0 讨论(0)
提交回复
热议问题