Hello, all:
This is my response to a little navel-gazing question about recursion in a programming class. I post it here because I enjoyed my response.
Programming is communication; the programmer “explains” to a computer how to carry out a task, with the explanation being the program. In this context, what do you understand by the term “Recursion”? Please share your thoughts!
In this context recursion actually makes a bit more sense. I mean, think about how people explain tasks to you. Most of the time you aren't given a step by step list of things to do which will invariably lead you to the completion of the task. Most of the time you're given general instructions on how to do a representative piece of the job, and description of what counts as "finished."
For an example of what I mean, consider mopping a floor, described iteratively and then recursively:
bool mopIterative(floor theFloor, mop myMop) {
for (int x=theFloor.xMin(); x<=theFloor.xMax(); x++)
for (int y=theFloor.yMin; y<=theFloor.yMax(); y++) { //Loop through the floor, mopping each coordinate
theFloor.goTo(x,y);
theFloor.mop(myMop);
}
return true;
}
bool mopRecursive(floor theFloor, mop myMop) {
if (theFloor.isEmpty()) return true; //If there is no remaining unmopped floor, you're done
else {
theFloor.goTo(); //Go to a random space in the floor
theFloor.mop(myMop); //Mop that space
return mopRecursive(theFloor.restOf(), myMop); //Now do that to the rest of the floor
}
}
In other words, my Grandma never handed me a mop and a coordinate grid telling me precisely in which order to mop her kitchen tiles. She just handed me a mop, told me what to do with it, and told me I'd be done when I'd mopped the whole thing.
--Jake out