Created
January 14, 2019 21:59
-
-
Save scott-fleischman/96ed01f9a27dbed6524999d166e1568a to your computer and use it in GitHub Desktop.
Simple State + 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
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