Skip to content

Instantly share code, notes, and snippets.

@jml
Created June 2, 2015 18:01
Show Gist options
  • Select an option

  • Save jml/497fca8b0c3e82090360 to your computer and use it in GitHub Desktop.

Select an option

Save jml/497fca8b0c3e82090360 to your computer and use it in GitHub Desktop.
Transform State computation to IO
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