News for hackers. Stuff that matters.
Re:Trying Haskell by chris_j on Sunday April 10, 2011 @11:08AM (#2429582)
Is it actually possible to implement quicksort in a pure functional language like Haskell (with immutable data structures)? Sorting in place would seem to involve mutating the list every time two values are swapped.
Re:Trying Haskell by danieldk on Sunday April 10, 2011 @12:17PM (#2429737)
Yes. For example, the vector package provides mutable arrays:

http://hackage.haskell.org/package/vector-0.7.0.1

Mutability is attained by using the ST monad. The ST monad uses mutable memory, but since it does not allow other interactions with the outside world, its value can be extracted (unlike the IO monad). When you are done with the modification of the vector, it can be frozen to obtain a pure vector.

The IO monad can also be used, but not if you want to return a pure value.

A good tutorial can be found at:

http://www.haskell.org/haskellwiki/Numeric_Haskell:_A_Vector...

I used mutable vectors in the ST monad in maximum entropy training software, and they are really performant.

Re:Trying Haskell by KirinDave on Sunday April 10, 2011 @01:28PM (#2429911)
What blew my mind about the ST monad is that despite its promise of single-threading, it still allows for recursive division of labor.

I was like, "Wait. Wait. Waaaiiiitt. How does it do that?"

Re:Trying Haskell by Hemospectrum on Sunday April 10, 2011 @12:06PM (#2429714)
Haskell is designed to quarantine side effects, not remove them altogether. There's a special monad for building data structures that are mutable at creation time but appear immutable outside that block of code. In normal circumstances, you'd probably still have to copy the contents of the input into a new array before sorting it, though. So, it would still be less memory efficient on large inputs.
Re:Trying Haskell by jmillikin on Sunday April 10, 2011 @11:57AM (#2429693)
Haskell has mutable data structures; you could easily implement in-place quicksort with vectors, arrays, or pointers.