On Jun 7, 2013, at 8:59 PM, ben <bfranchuk at jetnet.ab.ca> wrote:
I have no problems with iteration or subroutines, so why do we need recursion?
Recursion is a tool, just like iteration. In cases where it's useful the code is
clearer/cleaner than if it was written with say iteration.
Stack overflow is a bug just like any other and your point is?
Given the number of compilers I've written, I wouldn't write a parser that
wasn't recursive decent. It's easy to write that way and easy to read the
parser code (can't say that about a table driven parser).
Tree walks are another case where recursion is the way to go. Iteration
results in ugly looking code.
Also, different languages lend themselves to different ways of thinking
about a problem. Some problems are easier to express in some languages
than others. I think functional programming languages are along those
lines. There are a class of problems that can be solved easily (ok, easier)
in a functional language than in say a procedural language.
I think part of the problem with why folks perceive this topic difficult is
that they only have experience with a single class of languages. Unless
you've had to work in multiple *classes* of languages and really understand
the problems that a particular language class is trying to solve, folks will
try to use techniques from a previous language in the new language. It's
always easy to tell when that's done.
I remember when I was taking an APL course, there were a number of problems
that needed to be solved and were told at the beginning that we'd fail if we *ever*
used iteration. You really had to think about the problem differently.
TTFN - Guy