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 (atleast 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?
Are there advantages for using recursion over iteration - other than sometimes readability and elegance?
recursion
Well, it's usually less code.And to the extent that it's less code, it's less error-prone.In particular, recursion is very beneficial when the iterative solutions requires that you simulate recursion with a stack. Recursion acknowledges that the compiler already manages a stack to accomplish precisely what you need. When you start managing your own, not only are you likely re-introducing the function call overhead you intended to avoid; but you're re-inventing a wheel (with plenty of room for bugs) that already exists in a pretty bug-free form.IMO, only eschew recursion if it can be done naturally and easily without a stack. (Or other non-scalar state and/or scope management structure.)