Accumulator Recursion CS125 Spring 2007 Arthur Kantor
Last lecture: –Converting tail recursion to a while loop Today: –Converting any recursion to tail recursion
Example: fac(n) Is this in tail recursion form?
Example: fac(n) Is this in tail recursion form? No. We cannot convert it to a while loop directly.
Accumulator recursion We can convert fac(n) to accumulator recursion form. Accumulator recursion form is a kind of tail recursion form. In addition to the original parameters, it has parameters which hold the work completed so far.
Converting fac(n) to accumulator recursion form In fac(n), the work completed so far is the productSoFar So int fac(int n) becomes fac(int n, int productSoFar) The recursive case becomes
If product so far is available We can do the multiplication before the recursive call Previously we had to do the multiplication after the recursive call
Fac(n) in accumulator recursion form Is this in tail recursion form now? What must the productSoFar be in the first call to fac?
Fac(n) in accumulator recursion form Is this in tail recursion form now? Yes What must the productSoFar be in the first call to fac? fac(n,1)
Trace through two versions of fac
Converting to loop form Now we can convert fac(n, productSoFar) from acumulator recursion form to loop form
Converting to loop form Accumulator recursion form Loop form
Converting to loop form Accumulator recursion form Loop form
Converting to loop form Accumulator recursion form Loop form
Converting to loop form Accumulator recursion form Loop form
fac(n,productSoFar) must always be initially called with productSoFar =1 Convert productSoFar to a local variable outside of the loop Removing ‘extra’ parameters
Loop fac(n) vs. Recursive fac(n) ProductSoFar explicitly stores the work so far The work so far is implicitly stored in the stack of recursive calls
Another example: pow(base, exp) Is this recursive? Is this tail recursive?
Another example: pow(base, exp) Is this recursive? Yes Is this tail recursive? No What is the work so far?
Another example: pow(base, exp) Is this recursive? Yes Is this tail recursive? No What is the work so far? Again, it’s the incomplete product so far
pow(base, exp) Original –The multiplication is done after returning from the recursive call Accumulator Recursion –The multiplication is now done before the recursive call
What are the differences? Original Accumulator Recursion
What are the differences? Original Accumulator Recursion
pow(base, exp, productSoFar) Is this in tail recursive form now? What should productSoFar be in the first call to pow?
Is this in tail recursive form now? Yes What should productSoFar be in the first call to pow? 1 pow(base, exp, productSoFar)
Call trace
Preparing to convert to loop form Accumulator recursion form If-else statements switched
Converting to loop form Accumulator recursion form Loop form
Converting to loop form Accumulator recursion form Loop form
Converting to loop form Accumulator recursion form Loop form
Removing ‘extra’ arguments Loop form Final loop form