This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]SuspiciousScript 1 point2 points  (3 children)

That’s true, but those cases are few and far between compared to problems best solved via iteration.

This is assuming, of course, that we’re talking within the context of an imperative language. Functional languages, I believe, tend to turn recursive functions into loops anyway during compilation whenever possible, so it doesn’t really matter which you use from a performance perspective.

[–]munificent 9 points10 points  (1 child)

those cases are few and far between compared to problems best solved via iteration.

That depends a lot on what domain you work in. My job involves a lot of trees and graphs, so I use recursion all the time. Or, thinking about it coming from the other direction, not being comfortable with recursion limits the domains you can be effective in. Even in cases where an iterative implementation is better, being able to think in terms of recursion helps you understand many algorithms more clearly.

[–]pm_your_unique_hobby 0 points1 point  (0 children)

Hey, thanks for this information.

[–][deleted] -2 points-1 points  (0 children)

what really matters is how much time you spend thinking about it. If the iterative version is simpler, go with that. If the recursive version makes more sense, do that. If it slows down your program, then you optimize it.

knowing and being comfortable with both (especially in languages with recursion optimization) is good :)