Skip to content

Instantly share code, notes, and snippets.

@raichoo
Created July 20, 2016 19:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raichoo/f71e8f2e6ef4b6c01efc18ec565caac8 to your computer and use it in GitHub Desktop.
Save raichoo/f71e8f2e6ef4b6c01efc18ec565caac8 to your computer and use it in GitHub Desktop.
Sleepsort implementation in Haskell
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