Due Sun. Oct. 29, 2000 at 11:59 PM
In this lab you will discover one of the most powerful and fundamental programming techniques: recursion. Recursion is a way of programming the processing of multiple entities such that at any given moment, one is dealing with only the local issues, not the global issues of the entire collection of entities being processed. Recursion is a natural outgrowth of an inductive proof, as the illustrated by the classic "if you can get onto the first rung of a ladder and you know how to get from any arbitrary rung to the next, then you can climb the whole ladder." Recursion is a very important tool in the OOP arsenal, but is certainly not limited to OOP. It appears in procedural programming (C, Basic, Fortran, Pascal, etc), but perhaps more importantly, it is at the very foundation of functional programming languages such as Lisp and Scheme. When you take CS275, you will notice striking similarities between this lab and the first Scheme lab in CS275.
Recursion can be a very difficult concept to grasp and even harder to master. I suggest making lots of diagrams (object diagrams tend to be more useful here than class diagrams) and spend a lot of time discussing it with your classmates. And of course, raise any and all questions you have during class and lecture so the instructors can address them.
Try to understand recursion in terms of it being a local description of a global phenomenon.
Recursion works by having a method in one object call the exact same method in another object (which may be itself). If all the objects are linked in some manner, i.e. all have references to one other object in the collection, and all objects are referenced by some other object, then by the time the first method exits, all the objects will be accessed and processed.
In order to do recursion one needs a collection of objects where:
Below is an example of generally what is happening in three linked objects during the call to the recursive method of the first object:
One thing to notice about the above sequence is how any given object is forced to wait until all the successive objects have finished before it can finish. See the state of Object1 and Object 2 in Steps 2 and 3 above. These are called "pending calls" and often referred to being "stacked" (a reference to the stack implementation of recursion that is used in most modern microprocessers). Try viewing the above process like one of those New Year's party favors that rolls out then back in when you blow in it.
However, the most important thing to notice is
If you look back at the movie loop program, you will see that it was done with what was essentially a modified recursive methodology.
You might be asking yourself, "But what if I don't want to process the entire collection?" or "How can I tell if I am at the last object in the collection"? Good questions indeed.
Recursion always involves two different cases:
Hmm... we have two different situations that perform the same abstract process (the recursive method), but yet have different implementations....
What should the class (UML) diagram for a recursive process look like?
To see this, let us exam the "Lisp List", which is a linked list like the circular list, but is linear instead. See the first lab section on the left.
After you have completed your work on the Lisp List, you can create your own recursive methods for it in the second lab section on the left.
Warning: Do not attempt to write your own recursive methods until you fully understand how the example methods work!