Created
June 30, 2018 06:12
-
-
Save hsk/bff149c54eacfa15ff54d57e4341f774 to your computer and use it in GitHub Desktop.
coroutine 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
| module Main where | |
| import Control.Monad.State | |
| import Control.Monad.Coroutine | |
| import Control.Monad.Coroutine.SuspensionFunctors | |
| crst :: Coroutine (Yield Int) (StateT Int IO) Int | |
| crst = do v<-lift get;yield v;lift(modify(+1));liftIO(putStr "動く">>print(v*10)); return (v*10) | |
| runT :: MonadIO m=>Coroutine (Yield Int) m Int -> m Int | |
| runT c = resume c>>=g where | |
| g(Right e) = liftIO(putStr "foreverなら動かない">>print e)>> return e | |
| g(Left(Yield e c))=do | |
| liftIO(print e) | |
| if e >= 3 then return e else runT c | |
| main = do | |
| evalStateT (runT crst) 1 | |
| evalStateT (runT $ forever crst) 1 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1
動く10
foreverなら動かない10
1
動く10
2
動く20
3