Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a version, just to prove you can convert code from Wikipedia almost exactly into Haskell ;)</p> <pre><code>import Control.Monad.ST import Data.Array.ST import Data.Foldable import Control.Monad -- wiki-copied code starts here partition arr left right pivotIndex = do pivotValue &lt;- readArray arr pivotIndex swap arr pivotIndex right storeIndex &lt;- foreachWith [left..right-1] left (\i storeIndex -&gt; do val &lt;- readArray arr i if (val &lt;= pivotValue) then do swap arr i storeIndex return (storeIndex + 1) else do return storeIndex ) swap arr storeIndex right return storeIndex qsort arr left right = when (right &gt; left) $ do let pivotIndex = left + ((right-left) `div` 2) newPivot &lt;- partition arr left right pivotIndex qsort arr left (newPivot - 1) qsort arr (newPivot + 1) right -- wrapper to sort a list as an array sortList xs = runST $ do let lastIndex = length xs - 1 arr &lt;- newListArray (0,lastIndex) xs :: ST s (STUArray s Int Int) qsort arr 0 lastIndex newXs &lt;- getElems arr return newXs -- test example main = print $ sortList [212498,127,5981,2749812,74879,126,4,51,2412] -- helpers swap arr left right = do leftVal &lt;- readArray arr left rightVal &lt;- readArray arr right writeArray arr left rightVal writeArray arr right leftVal -- foreachWith takes a list, and a value that can be modified by the function, and -- it returns the modified value after mapping the function over the list. foreachWith xs v f = foldlM (flip f) v xs </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload