Are there advantages for using recursion over iteration - other than sometimes readability and elegance?
- by Prog
I am about to make two assumptions. Please correct me if they're wrong:
There isn't a recursive algorithm without an iterative equivalent.
Iteration is always cheaper performance-wise than recursion (at
least in general purpose languages such as Java, C++, Python etc.).
If it's true that recursion is always more costly than iteration, and that it can always be replaced with an iterative algorithm (in languages that allow it) - than I think that the two remaining reasons to use recursion are: elegance and readability.
Some algorithms are expressed more elegantly with recursion. E.g. scanning a binary tree.
However apart from that, are there any reasons to use recursion over iteration? Does recursion have advantages over iteration other than sometimes elegance and readability?