Skip to content

Instantly share code, notes, and snippets.

@hsk
Created June 30, 2018 06:12
Show Gist options
  • Select an option

  • Save hsk/bff149c54eacfa15ff54d57e4341f774 to your computer and use it in GitHub Desktop.

Select an option

Save hsk/bff149c54eacfa15ff54d57e4341f774 to your computer and use it in GitHub Desktop.
coroutine forever
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
@hsk
Copy link
Author

hsk commented Jun 30, 2018

1
動く10
foreverなら動かない10
1
動く10
2
動く20
3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment