05-recursion-is-iteration.student

05-recursion-is-iteration.student - Last Time The testing...

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

View Full Document Right Arrow Icon
Last Time: * The testing process * The problem of "nearly identical" functions * Function pointer syntax * Generalization via function pointers Today: * The tail-recursion/iteration duality * "Proof by construction" ************************************************************** So far, we've talked about several closely related ideas: 1: General recursion 2: Tail recursion 3: Iteration A recursive problem is one that is defined in terms of itself. There are two important features---that there be one or more trivial base cases, and that there be a way to solve "big" instances by first solving "smaller" instances, then performing a simple computation. Recursive problems usually follow the structure of an inductive proof. Once a problem has been stated in recursive terms, it is fairly simple to write a recursive program to solve it. Unfortunately, such programs often require one stack frame for every recursive call; this can be substantial. To solve this, we considered tail recursive programs. The essential nature of a tail recursive solution is that it requires only "constant space"---it doesn't matter how "large" the problem to be solved is, we only need a fixed number of stack frames to solve it, as long as our compiler is sufficiently clever. For example, in our tail-recursive factorial solution, we only need the first fact_helper stack frame, no matter how large N happens to be. That turns out to also be true for iterative solutions of the same problems. In fact, the two versions of factorial() we've seen are very very similar. It turns out that that similarity is no accident! Outline for today 1) Get a little practice in "proof by construction" 2) Prove that tail-recursion is a subset of iteration. We'll show that for *any* tail-recursive function, you can write an iterative equivalent. In other words, tail-recursion is *just* iteration. The equivalence we will show is *not* true for general recursive programs---in particular, there are some recursive programs that cannot be solved in constant space, including the tree traversal problem from project 2. ++++++++++++++++++++++++++++++++++++++++++ The general idea behind what we are trying to do today is simple. We want to show that tail-recursion is "no more powerful" than iteration---that for any computation we could express tail-recursively, we could also express it iteratively. We do this by giving a method for converting an arbitrary tail-recursive function to
Image of page 1

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

View Full Document Right Arrow Icon
an iterative one. This technique is called "proof by construction." To see how it works, let's start with an easy one. C++ has three different kinds of loops: while, do, and for: while (<boolean-condition>) <body-statement> On entry, the condition is evaluated. If the condition is false, the loop terminates. Otherwise, the body statement is evaluated, and the loop is re-entered. do <body-statement> while (<boolean-expression>); On entry, the body statement is evaluated. Then, the condition.
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