On 10/20/2005 at 4:03 PM Sridhar Ayengar wrote:
The right tool for the right job, and all.
Actually, this dovetails very neatly with the topic and perhaps illustrates
the difference in thought between the C/Java/Algol and the FORTRAN camps.
It all depends on where you live (mentally).
At least up until F77, recursion was not a part of the language--and in
fact, was prohibited by many implementations. To a great extent, this
hinged on the lack of a hardware-facilitated stack. For many machines, the
subroutine call sequence looked like this:
EXIT: JUMP *-*
ENTRY: first instruction
....
JUMP EXIT
The machine's CALL instruction would simply store the return address in the
location just before the subroutine's entry point. Yes, I know--it's code
modification, but it wasn't such a pariah until relatively recent times.
Storage local to the subroutine (as well as any memory needed to save the
caller's context) was kept in static space. You work with what you've
got (try implementing a recursive call mechanism with local storage on an
IBM 1620 Model I).
Tens, if not hundreds of millions of lines of production code was written
this way.
Pure recursion was not unknown, but was generally held to be something for
the textbooks.
While recursion was not generally used, occasionally routines needed to
exhibit parallel reentrancy, particularly in the case of event processing,
such as I/O and so did need to maintain some sort of stack structure. This
sort of thing was usually dreaded by programmers, not because of the
inability to code such stuff, but rather because of the difficulty in
debugging reentrant code. I believe that this still holds today--and IMOHO
the same applies for debugging recursive code failures.
On the other hand, an iterative code overflow generally allows for one to
make a graceful exit and preserve of the history of the loop. There's
rarely any question about "how do I get back to the point of origin?" (i.e.
unwind the stack).
I remember in a SNOBOL programming course taught by none other than Bob
Dewar, the "Tower of Hanoi" example was trotted out. Immediately, one of
the students pointed out that it was far easier to write an iterative
solution in just about any other language than SNOBOL--and perhaps the real
problem was that SNOBOL lacked good iteration facilities.
Suffice it to say that Dr. Dewar was not amused.
I think this illustrates perfectly how thinking has gradually shifted over
the years. Who knows what common practice today will become the b?te
noir of tomorrow? Perhaps state machine models will be the next trend and
both iteration and recursion will be viewed as hopelessly quaint...
Cheers,
Chuck