{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

03-tailrecursion.student - Last Time The stack calling...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Last Time: * The stack: calling multiple funcitons * Recursion: base case, inductive step * Recursive functions: factorial, numOnes Today: * The recursion "space problem": * what it is, and how to fix it * Tail-recursion: a "small" recursive twist * Testing programs ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ So far, we've introduced the notion of recursive problems and, correspondingly, recursive functions. Something is recursive if it refers to itself. A problem is recursive if it has: * One or more trivial base cases * Other cases defined in terms of smaller problem instances. (these are "inductive steps") A function is recursive if it calls itself. We then gave a few examples of how to map from recursive definitions to recursive functions: identify the base case, and write that down. Then, write the inductive step as a recursive call. Last time, I left you with the following question: Let's consider the resource costs of the recursive version of factorial: int factorial(int x) { if (x == 0) { return 1; } else { return x * factorial(x-1); } } Compare this to a version with a loop: int fact_iter(int x) { int result = 1; while (x) { result *= x; x--; } return result; } Questions for the class, small groups ~5 minutes: * How many multiplications does each version perform? * How much space does each one require?
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The recursive version is much less efficient in terms of space! Happily, there is a way to re-write the recursive version to use (approximately) the same amount of space as is required by the iterative version. Consider the following implementation: int fact_helper(int n, int result) // REQUIRES: n >= 0 // EFFECTS: returns result * n! { if (n == 0) { return result; } else { return fact_helper(n-1, result * n); } } int factorial(int num) // REQUIRES: n >= 0 // EFFECTS: returns num! { return fact_helper(num, 1); } This function is equivalent to the original factorial---we'll prove this by induction. There are two steps to any indutive proof. First, prove the base case, and second, the inductive step. First, let's state the hypothesis: fact_helper (n, r) = n! * r Naturally, if this is true, then fact_helper(n, 1) would be n!, and factorial() would be correct. So, it's a pretty useful hypothesis. ;-) First, the base case: if n=0, the fact_helper(0,r) = r = 1 * r = (0!) * r so, the hypothesis holds for the base case.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern