Example Programs
Take this Scheme program as an example:
;; factorial : number -> number ;; to calculate the product of all positive ;; integers less than or equal to n. (define (factorial n) (if (= n 0) 1 (* n (factorial (- n 1)))))This program is not written in a tail recursion style. Now take this Scheme program as an example:
;; factorial : number -> number ;; to calculate the product of all positive ;; integers less than or equal to n. (define (factorial n) (let fact ( ) (if (zero? i) acc (fact (- i 1) (* acc i)))))The inner procedure fact calls itself last in the control flow. This allows an interpreter or compiler to reorganize the execution which would ordinarily look like this:
into the more efficient variant, in terms of both space and time:
call factorial (3) call fact (3 1) replace arguments with (2 3), jump to "fact" replace arguments with (1 6), jump to "fact" replace arguments with (0 6), jump to "fact" return 6 return 6This reorganization saves space because no state except for the calling function's address needs to be saved, either on the stack or on the heap, and the call stack frame for fact is reused for the intermediate results storage. This also means that the programmer need not worry about running out of stack or heap space for extremely deep recursions. It is also worth noting, in typical implementations, the tail recursive variant will be substantially faster than the other variant, but only by a constant factor.
Some programmers working in functional languages will rewrite recursive code to be tail-recursive so they can take advantage of this feature. This often requires addition of an "accumulator" argument (acc in the above example) to the function. In some cases (such as filtering lists) and in some languages, full tail recursion may require a function that was previously purely functional to be written such that it mutates references stored in other variables.
An example in pseudo-C follows. Suppose we have the following functions:
int a(int x, int y) { foobar(x, y); return b(x + 1, y + 2); } int b(int u, int v) { foobar(u, v); return u + v; }Function a can be changed to:
There are possible aliasing problems but this is the basic idea.
Read more about this topic: Tail Call
Famous quotes containing the word programs:
“Although good early childhood programs can benefit all children, they are not a quick fix for all of societys illsfrom crime in the streets to adolescent pregnancy, from school failure to unemployment. We must emphasize that good quality early childhood programs can help change the social and educational outcomes for many children, but they are not a panacea; they cannot ameliorate the effects of all harmful social and psychological environments.”
—Barbara Bowman (20th century)
“Short of a wholesale reform of college athleticsa complete breakdown of the whole system that is now focused on money and powerthe womens programs are just as doomed as the mens are to move further and further away from the academic mission of their colleges.... We have to decide if thats the kind of success for womens sports that we want.”
—Christine H. B. Grant, U.S. university athletic director. As quoted in the Chronicle of Higher Education, p. A42 (May 12, 1993)