Skip to content

Instantly share code, notes, and snippets.

@ra1u
Created June 15, 2016 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ra1u/0d2a9c5c090e4a9fbb0fed71eaba400c to your computer and use it in GitHub Desktop.
Save ra1u/0d2a9c5c090e4a9fbb0fed71eaba400c to your computer and use it in GitHub Desktop.
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Monad.Reader
evalT :: (Monad m) => (b -> m a) -> b -> ([a] -> [b]) -> m [b]
evalT mf def f = r where
am = r >>= traverse mf -- :: m [a]
bm = fmap f am -- :: m [b]
r = fmap (def:) bm -- :: m [b]
-- state monad stucks
stateM n = modify ((+) n) >> get
main = print $ take 10 $ evalState (evalT stateM 0 id) 1
-- reader monad works
readerM n = reader (+n)
main2 = print $ take 10 $ runReader (evalT readerM 0 id) 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment