3 ms·
One big practical complaint is that recursion makes it difficult to reason about the run-time characteristics of code. There's some truth to this - I'm personal
by bunderbunder 11y ago
One big practical complaint is that recursion makes it difficult to reason about the run-time characteristics of code. There's some truth to this - I'm personally no stranger to refactoring recursive functions that have proven susceptible to stack overflows into iterative implementations.
I think even the most die-hard functional programming fans among us secretly know that this is a problem. It's why virtually every functional language implements tail call optimization, and why one of the earliest things that's taught to new students of functional programming is how to ensure that functions use tail recursion whenever possible. Sometimes we even go through some pretty impressive contortions because of this. Consider the case of continuation passing style.
The other is that a lot of people find recursion hard to understand. Others seem to naturally think in terms of recursion and think iteration is a headache. I'm not sure the two camps ever see eye to eye. It might be a brain wiring thing. Though perhaps practice rewires your brain - I wouldn't be surprised if making someone spend a decade writing mostly imperative code causes them to become more comfortable with iteration, and making someone spend a decade writing mostly declarative code causes them to become more comfortable with recursion.
- Swizec 11y ago> I wouldn't be surprised if making someone spend a decade writing mostly imperative code causes them to become more comfortable with iteration, and making someone spend a decade writing mostly declarative code causes them to become more comfortable with recursion. I have spent a decade doing a mix of both. It really really really depends on the problem at hand what's easier to understand Have to traverse a tree? Recursion. Have to make a big thing out of small things? Recursion. Have to add +1 to a bunch of numbers? Loop.
- smosher_ 11y agoFwiw I avoid writing both loops and recursion as much as possible. Collection data structures usually come with their own fold/map/etc functions, so this works well in practice.