fork download
  1. import Data.IORef
  2. import System.IO.Unsafe (unsafePerformIO)
  3.  
  4. getGlobalVar :: IO Int
  5. setGlobalVar :: Int -> IO ()
  6.  
  7. (getGlobalVar,setGlobalVar) = unsafePerformIO $ do
  8. x_ref <- newIORef 0
  9. return (readIORef x_ref, writeIORef x_ref)
  10.  
  11. foo = do v <- (+1) <$> getGlobalVar
  12. setGlobalVar v
  13.  
  14. bar = do v <- (*2) <$> getGlobalVar
  15. setGlobalVar v
  16.  
  17. main = do
  18. print =<< foo
  19. print =<< bar
  20. print =<< foo
  21. print =<< bar
  22.  
Success #stdin #stdout 0s 8388607KB
stdin
Standard input is empty
stdout
1
2
3
6