4 ms·
> recursion, despite it cleanliness, improves readability at a significant cost in memory Tradeoffs like that are what highly dynamic languages are all about.
by prodigal_erik 15y ago
> recursion, despite it cleanliness, improves readability at a significant cost in memory
Tradeoffs like that are what highly dynamic languages are all about. You shouldn't be using Python (yet) if the natural expression of your algorithm is too expensive for your problem size (with today's implementations). Making your Python non-idiomatic and hard to read will cost you its advantages, and won't even buy you much time before you find yourself porting to Java or something similarly optimized.
- baddox 15y agoWhat you say is true, but you seem to be implying that only recursive algorithms are idiomatic in Python. I would disagree: pretty much anything you do on sequential data in Python is done idiomatically with a for loop or a comprehension, neither of which are particularly recursive.
- DougBTX 15y ago> you seem to be implying that only recursive algorithms are idiomatic in Python. He didn't say that, so thankfully there's nothing to disagree with.
- baddox 15y agoRe-read what he said. It's a pretty clear implication that recursive code is idiomatic in Python.
- prodigal_erik 15y agoSome algorithms solve a problem by examining solutions to other instances of the same problem. Minimax and quicksort work this way, as do a lot of tree and graph traversal problems. Implementations of these algorithms are inherently recursive, in the sense that you're going to need to say "carry out the whole algorithm again here". You can always fake recursion by creating objects to hold the state of the call graph (or stack, if you don't have continuations), but that's so cumbersome to write or read that you'll quickly be better off in a language that doesn't force you to do it, just as we've now all adopted languages which actually provide functions rather than faking it by saving and restoring variables around GOTOs (I wish I were kidding).