Skip to content

Instantly share code, notes, and snippets.

@scott-fleischman
Created January 14, 2019 21:59
Show Gist options
  • Save scott-fleischman/96ed01f9a27dbed6524999d166e1568a to your computer and use it in GitHub Desktop.
Save scott-fleischman/96ed01f9a27dbed6524999d166e1568a to your computer and use it in GitHub Desktop.
Simple State + IO
module Main where
import Control.Monad.Trans.State.Lazy
import Control.Monad.IO.Class (liftIO)
nextIntAndPrint :: StateT Int IO Int
nextIntAndPrint = do
currentValue <- get
let newValue = succ currentValue
put newValue
liftIO $ print newValue
return newValue
intSeq :: Num s => Monad m => StateT s m a -> m s
intSeq = flip execStateT 0
main :: IO ()
main = do
intSeq $ do
nextIntAndPrint
nextIntAndPrint
nextIntAndPrint
intSeq $ do
nextIntAndPrint
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment