| I feel like you're conflating "purity" with algorithms. Just to clarify, in Haskell there is no issue with algorithms being contained inside functions. Here's a naive implementation of quicksort: qsort [] = []
qsort (x:xs) = qsort smaller ++ [x] ++ qsort larger
where
smaller = [a | a <- xs, a <= x]
larger = [b | b <- xs, b > x]
Nothing about this algorithm requires side-effects, it doesn't mutate any state, it doesn't make use of counters. You can implement it anywhere inside of a Haskell program.People may charitably point out that this is a pretty bad naive implementation, because it doesn't just mutate values in the list up and down, which is inefficient. People uncharitably will take issue with even calling it quicksort, as some people believe that the property of just mutating values in the list is an integral part of it being quicksort. I'm not smart enough to have an opinion about whether or not it's a "true" quicksort. But the point is that you can absolutely have algorithms in Haskell that are not restricted to only run in a Monad. Monads exist in Haskell as a way to reason about side-effects. Whether or not an algorithm (or any function) needs to be executed inside a monad is a function of its use of side-effects, it's not fundamental. |
I do not "see" any clear algorithm in the code (no step-by-step procedure). I see recursion only.
¿Can you explain in words what this code is doing?
¿Can you implement this without using recursion?