3 ms·
Here's a direct translation of a C++ implementation into Haskell with no optimization: https://koerbitz.me/posts/Efficient-Quicksort-in-Haskell.html https://koe
by agentultra 4y ago
Here's a direct translation of a C++ implementation into Haskell with no optimization: https://koerbitz.me/posts/Efficient-Quicksort-in-Haskell.html https://koerbitz.me/posts/Efficient-Quicksort-in-Haskell.htm...
Personally I think the Haskell code is a bit nicer. Mutation is inherently unsafe and you need to be more careful when performing such computations. Haskell code makes this painful to read by planting red-flag words like, "unsafe" around.
In C++ mutation is the norm and so it's much easier skip past such code. No indication that these operations are unsafe. The experienced reader will scrutinize this carefully or gloss over it at their own peril. The code is terse on the subject as the syntax maximizes efficiency for this case.
Here's another: https://stackoverflow.com/a/5269180 https://stackoverflow.com/a/5269180
If you absolutely need in-place mutation, it's available. It's just not the default.
- hnfong 4y agoYes, but throwaway17_17 was making a philosophical point that imperative algorithms are somehow unnecessary, an out-of-date style of teaching, or merely a compiler detail, instead of being inherent to some algorithms. But I don't think there's a "non-imperative" quicksort algorithm. There's definitely value in preferring functional, non-mutating implementations over imperative where they make sense, but I've seen too many people pretty much claim that functional style of programming is all you need, and that's pretty much a lie IMHO.