Last active
March 13, 2018 09:31
-
-
Save imaximix/9f61f3e27f0b15232f91d08714a1cfc7 to your computer and use it in GitHub Desktop.
Implement Insertion Sort in Haskell to understand ST mutation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad.ST | |
import qualified Data.STRef as STR | |
import qualified Data.Array.ST as STArray | |
import qualified Data.Array as Arr | |
import qualified Data.Array.MArray as MA | |
untilM :: (Monad m) => m Bool -> m a -> m () | |
untilM p f = go | |
where go = do | |
x <- p | |
if x | |
then return () | |
else f >> go | |
insertionSort unordered = ordered | |
where | |
ordered = STArray.runSTArray $ do | |
indexRef <- STR.newSTRef 1 :: ST s (STR.STRef s Integer) | |
a <- MA.thaw unordered | |
untilM (do | |
index <- STR.readSTRef indexRef | |
return (index == 5) | |
) (do | |
elements <- MA.getElems a | |
index <- STR.readSTRef indexRef | |
elemAtIndex <- MA.readArray a index | |
nextElement <- MA.readArray a $ index + 1 | |
if (elemAtIndex < nextElement) | |
then STR.writeSTRef indexRef (index + 1) | |
else do MA.writeArray a index nextElement | |
MA.writeArray a (index + 1) elemAtIndex | |
STR.writeSTRef indexRef $ max 1 (index - 1) | |
) | |
return a | |
main = print $ insertionSort $ Arr.listArray (1, 5) [5,4,3,2,1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment