Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Necessary" Uses of Recursion in Imperative Languages

I've recently seen in a couple of different places comments along the lines of, "I learned about recursion in school, but have never used it or felt the need for it since then." (Recursion seems to be a popular example of "book learning" amongst a certain group of programmers.)

Well, it's true that in imperative languages such as Java and Ruby[1], we generally use iteration and avoid recursion, in part because of the risk of stack overflows, and in part because it's the style most programmers in those languages are used to.

Now I know that, strictly speaking, there are no "necessary" uses of recursion in such languages: one can always somehow replace recursion with iteration, no matter how complex things get. By "necessary" here, I'm talking about the following:

Can you think of any particular examples of code in such languages where recursion was so much better than iteration (for reasons of clarity, efficiency, or otherwise) that you used recursion anyway, and converting to iteration would have been a big loss?

Recursively walking trees has been mentioned several times in the answers: what was it exactly about your particular use of it that made recursion better than using a library-defined iterator, had it been available?

[1]: Yes, I know that these are also object-oriented languages. That's not directly relevant to this question, however.

like image 787
cjs Avatar asked Jun 18 '09 08:06

cjs


People also ask

What are the uses of recursion function?

Recursion is a technique used to solve computer problems by creating a function that calls itself until your program achieves the desired result.

What is the main reason to use recursion?

The great thing about recursion is that it divides the big problem into smaller parts and then the programmer can solve each of them individually by writing a function for those smaller problems. However, when we have to solve a problem with functions, the function has to solve it by calling itself.

What are the necessary conditions for recursion?

Like the robots of Asimov, all recursive algorithms must obey three important laws: A recursive algorithm must call itself, recursively. A recursive algorithm must have a base case. A recursive algorithm must change its state and move toward the base case.

Why is recursion is important feature in a language?

The notion of Recursion is so important to the study of language because it explains the human competence of generating (i) infinite sentences i.e. we can always add modifiers to constituents to make the sentence longer (ii) an infinite number of different sentences embedded in another sentence.


2 Answers

There are no "necessary" uses of recursion. All recursive algorithms can be converted to iterative ones. I seem to recall a stack being necessary, but I can't recall the exact construction off the top of my head.

Practically speaking, if you're not using recursion for the following (even in imperative languages) you're a little mad:

  1. Tree traversal
  2. Graphs
  3. Lexing/Parsing
  4. Sorting
like image 169
Kevin Montrose Avatar answered Nov 09 '22 12:11

Kevin Montrose


When you are walking any kind of tree structure, for example

  • parsing a grammar using a recursive-descent parser
  • walking a DOM tree (e.g. parsed HTML or XML)

also, every toString() method that calls the toString() of the object members can be considered recursive, too. All object serializing algorithms are recursive.

like image 38
mfx Avatar answered Nov 09 '22 14:11

mfx