Created
June 2, 2015 18:01
-
-
Save jml/497fca8b0c3e82090360 to your computer and use it in GitHub Desktop.
Transform State computation to IO
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
| import Control.Monad.State | |
| import Data.IORef | |
| pop :: State [a] a | |
| pop = do | |
| x:xs <- get | |
| put xs | |
| return x | |
| popper :: [a] -> IO (IO a) | |
| popper ys = popper' ys pop | |
| popper' :: s -> State s a -> IO (IO a) | |
| popper' initial action = do | |
| stack <- newIORef initial | |
| return $ do | |
| current <- readIORef stack | |
| let (x, xs) = runState action current | |
| writeIORef stack xs | |
| return x | |
| main :: IO () | |
| main = do | |
| p <- popper [(1 :: Int)..5] | |
| p >>= print | |
| p >>= print | |
| p >>= print | |
| p >>= print | |
| p >>= print | |
| p >>= print |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment