@@ -1,7 +1,9 @@
11{-# LANGUAGE CPP #-}
22module Countdown where
33
4+import Control.Monad.ST
45import Data.Functor.Identity
6+import Data.STRef
57
68-- eff
79#ifdef VERSION_eff
@@ -47,6 +49,26 @@ countdownRef :: Integer -> (Integer, Integer)
4749countdownRef n = if n <= 0 then (n, n) else countdownRef $ n - 1
4850{-# NOINLINE countdownRef #-}
4951
52+----------------------------------------
53+-- ST
54+
55+programST :: STRef s Integer -> ST s Integer
56+programST ref = do
57+ n <- readSTRef ref
58+ if n <= 0
59+ then pure n
60+ else do
61+ writeSTRef ref $! n - 1
62+ programST ref
63+{-# NOINLINE programST #-}
64+
65+countdownST :: Integer -> (Integer, Integer)
66+countdownST n = runST $ do
67+ ref <- newSTRef n
68+ a <- programST ref
69+ s <- readSTRef ref
70+ pure (a, s)
71+
5072----------------------------------------
5173-- mtl
5274