Top Topics
-
Sleep
906 recent check-ins -
LAK at NJD 05/30/2012
870 recent check-ins -
NBA Playoffs
498 recent check-ins -
Boston Celtics
460 recent check-ins -
Miami Heat
285 recent check-ins
-
Your Review
Loading - Loading
0 people checked-in to Tail recursion on GetGlue
Check-in to entertainment with GetGlue. Connect with friends, discover new favorites, and unlock FREE stickers and discounts.
In computer science, tail recursion (or tail-end recursion) is a special case of recursion in which the last operation of the function, the tail call, is a recursive call. Such recursions can be easily transformed to iterations. Replacing recursion with iteration, manually or automatically, can drastically decrease the amount of stack space used and improve efficiency.
This technique is commonly used with functional programming languages, where the declarative approach and explicit handling of state promote the use of recursive functions that would otherwise rapidly fill the call stack. When a function is called, the computer must "remember" the place it was called from, the return address, so that it can return to that location with the result once the call is complete. Typically, this information is saved on the stack, a simple list of return locations in order of the times that the call locations they describe were reached.
Sometimes, the last thing that a function does after completing all other operations is to simply call a function, possibly itself, and return its result. With tail recursion, there is no need to remember the place we are calling from — instead, we can leave the stack alone, and the newly called function will return its result directly to the original caller. Converting a call to a branch or jump in such a case is called a tail call optimization.
Note that the tail call doesn't have to appear lexically after all other statements in the source code; it is only important that its result be immediately returned, since the calling function will never get a chance to do anything after the call if the optimization is performed. For normal, non-recursive function calls, this is usually a micro-optimization that saves little time and space, since there are not that many different functions available to call. When dealing with recursive or mutually recursive functions, however, the stack space and the number of returns saved can grow to huge numbers, since a function can call itself, directly or indirectly, a huge number of times.
In fact, it often asymptotically reduces stack space requirements from linear, or O(n), to constant, or O(1).
Similar to 0 things you like:
Sleep
LAK at NJD 05/30/2012
NBA Playoffs
Boston Celtics
Miami Heat
Check-in to entertainment with GetGlue. Connect with friends, discover new favorites, and unlock FREE stickers and discounts.
You can edit this page because you have earned special privileges on Glue.
Only make changes if you are certain that they are correct.
Made in New York City | Copyright 2009-2012, AdaptiveBlue, Inc