Presentation is loading. Please wait.

Presentation is loading. Please wait.

Accumulator Recursion CS125 Spring 2007 Arthur Kantor.

Similar presentations


Presentation on theme: "Accumulator Recursion CS125 Spring 2007 Arthur Kantor."— Presentation transcript:

1 Accumulator Recursion CS125 Spring 2007 Arthur Kantor

2 Last lecture: –Converting tail recursion to a while loop Today: –Converting any recursion to tail recursion

3 Example: fac(n) Is this in tail recursion form?

4 Example: fac(n) Is this in tail recursion form? No. We cannot convert it to a while loop directly.

5 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.

6 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

7 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

8 Fac(n) in accumulator recursion form Is this in tail recursion form now? What must the productSoFar be in the first call to fac?

9 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)

10 Trace through two versions of fac

11

12

13

14

15

16

17 Converting to loop form Now we can convert fac(n, productSoFar) from acumulator recursion form to loop form

18 Converting to loop form Accumulator recursion form Loop form

19 Converting to loop form Accumulator recursion form Loop form

20 Converting to loop form Accumulator recursion form Loop form

21 Converting to loop form Accumulator recursion form Loop form

22 fac(n,productSoFar) must always be initially called with productSoFar =1 Convert productSoFar to a local variable outside of the loop Removing ‘extra’ parameters

23 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

24 Another example: pow(base, exp) Is this recursive? Is this tail recursive?

25 Another example: pow(base, exp) Is this recursive? Yes Is this tail recursive? No What is the work so far?

26 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

27 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

28 What are the differences? Original Accumulator Recursion

29 What are the differences? Original Accumulator Recursion

30 pow(base, exp, productSoFar) Is this in tail recursive form now? What should productSoFar be in the first call to pow?

31 Is this in tail recursive form now? Yes What should productSoFar be in the first call to pow? 1 pow(base, exp, productSoFar)

32 Call trace

33

34

35

36

37

38

39 Preparing to convert to loop form Accumulator recursion form If-else statements switched

40 Converting to loop form Accumulator recursion form Loop form

41 Converting to loop form Accumulator recursion form Loop form

42 Converting to loop form Accumulator recursion form Loop form

43 Removing ‘extra’ arguments Loop form Final loop form


Download ppt "Accumulator Recursion CS125 Spring 2007 Arthur Kantor."

Similar presentations


Ads by Google