05-recursion-is-iteration.student - Last Time: * The...

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/14/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 8

05-recursion-is-iteration.student - Last Time: * The...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online