In Haskell, when we use a do block, how does it figure out which monad to be used? -
we know block syntactic sugar. how figure out monadic context in? assume don't use <-
operator anywhere in block.
maybe "practical" examples help:
foo1 = print 5 return 7 -- print belongs io monad. whole thing in io. foo2 x = writetvar x 7 return 11 -- writetvar belongs stm monad. whole thing in stm. foo3 = let x = 5 [1, 2, 3, 4] -- last line list expression. whole thing in list monad. foo4 = put 7 return 9 -- put in state monad. whole thing in state monad. foo5 = x <- magic1 y <- magic2 return (x, y) -- in whatever monad magic1 , magic2 in. foo6 = return 13 -- doesn't mention monad. works possible monads! foo7 abc def = x <- abc y <- def return (x, y) -- runs in whatever monad abc , def run in. -- passing different arguments, can change monad is!
Comments
Post a Comment