-
-
Save kbridge/64d01f9160e5689913cd5b60d6bb30e0 to your computer and use it in GitHub Desktop.
demostrate monad transformers and forever
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 | |
import Control.Monad.Trans.Class -- from `transformers` | |
import Control.Monad.Trans.Maybe -- from `transformers` | |
import Data.IORef | |
main :: IO () | |
main = do | |
i <- newIORef (0 :: Int) | |
void $ runMaybeT $ forever $ do | |
n <- lift (readIORef i) | |
if n < 5 | |
then lift $ do | |
print n | |
modifyIORef i (+1) | |
else | |
MaybeT $ return Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
even better, with
guard