Created
July 20, 2016 19:24
-
-
Save raichoo/f71e8f2e6ef4b6c01efc18ec565caac8 to your computer and use it in GitHub Desktop.
Sleepsort implementation in Haskell
This file contains hidden or 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 Data.Foldable | |
import Control.Concurrent | |
import Control.Monad | |
import System.IO.Unsafe | |
sleepSort :: [Int] -> [Int] | |
sleepSort xs = unsafePerformIO $ do | |
res <- newMVar [] | |
ws <- traverse (sleepSort' res) xs | |
traverse_ takeMVar ws | |
takeMVar res | |
where | |
sleepSort' res x = do | |
w <- newEmptyMVar | |
void $ forkIO $ do | |
threadDelay (x * 1000000) | |
print x -- just so you can see the magic happen | |
modifyMVar_ res (return . (++ [x])) | |
putMVar w () | |
return w | |
main :: IO () | |
main = print (sleepSort [7,8,4,5,6,9,1,2,3,0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment