What is tail recursion?

前端 未结 28 3579
一个人的身影
一个人的身影 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:29

    A tail recursive function is a recursive function where the last operation it does before returning is make the recursive function call. That is, the return value of the recursive function call is immediately returned. For example, your code would look like this:

    def recursiveFunction(some_params):
        # some code here
        return recursiveFunction(some_args)
        # no code after the return statement
    

    Compilers and interpreters that implement tail call optimization or tail call elimination can optimize recursive code to prevent stack overflows. If your compiler or interpreter doesn't implement tail call optimization (such as the CPython interpreter) there is no additional benefit to writing your code this way.

    For example, this is a standard recursive factorial function in Python:

    def factorial(number):
        if number == 1:
            # BASE CASE
            return 1
        else:
            # RECURSIVE CASE
            # Note that `number *` happens *after* the recursive call.
            # This means that this is *not* tail call recursion.
            return number * factorial(number - 1)
    

    And this is a tail call recursive version of the factorial function:

    def factorial(number, accumulator=1):
        if number == 0:
            # BASE CASE
            return accumulator
        else:
            # RECURSIVE CASE
            # There's no code after the recursive call.
            # This is tail call recursion:
            return factorial(number - 1, number * accumulator)
    print(factorial(5))
    

    (Note that even though this is Python code, the CPython interpreter doesn't do tail call optimization, so arranging your code like this confers no runtime benefit.)

    You may have to make your code a bit more unreadable to make use of tail call optimization, as shown in the factorial example. (For example, the base case is now a bit unintuitive, and the accumulator parameter is effectively used as a sort of global variable.)

    But the benefit of tail call optimization is that it prevents stack overflow errors. (I'll note that you can get this same benefit by using an iterative algorithm instead of a recursive one.)

    Stack overflows are caused when the call stack has had too many frame objects pushed onto. A frame object is pushed onto the call stack when a function is called, and popped off the call stack when the function returns. Frame objects contain info such as local variables and what line of code to return to when the function returns.

    If your recursive function makes too many recursive calls without returning, the call stack can exceed its frame object limit. (The number varies by platform; in Python it is 1000 frame objects by default.) This causes a stack overflow error. (Hey, that's where the name of this website comes from!)

    However, if the last thing your recursive function does is make the recursive call and return its return value, then there's no reason it needs to keep the current frame object needs to stay on the call stack. After all, if there's no code after the recursive function call, there's no reason to hang on to the current frame object's local variables. So we can get rid of the current frame object immediately rather than keep it on the call stack. The end result of this is that your call stack doesn't grow in size, and thus cannot stack overflow.

    A compiler or interpreter must have tail call optimization as a feature for it to be able to recognize when tail call optimization can be applied. Even then, you may have rearrange the code in your recursive function to make use of tail call optimization, and it's up to you if this potential decrease in readability is worth the optimization.

提交回复
热议问题