スリープソート
リストを返す版
{-# LANGUAGE BlockArguments #-}
import Control.Concurrent
import Control.Concurrent.STM
import Data.Function
sort :: [Int] -> IO [Int]
sort ns = do
c <- atomically newTChan
mapM_ (forkIO . single c) ns
threadDelay 1000000
atomically $ fix \go -> do
e <- isEmptyTChan c
if e then pure [] else (:) <$> readTChan c <*> go
single :: TChan Int -> Int -> IO ()
single c n = threadDelay (n * 1000) >> atomically (writeTChan c n)