我正在试验MaybeT
monad,特别是MaybeT Identity String
import Control.Monad.Trans.Maybe
import Control.Monad.Identity
import Data.Maybe
main :: IO ()
main = putStrLn . show . runIdentity . runMaybeT $ maybeGetString
maybeGetString :: MaybeT Identity String
maybeGetString = return "first" >>
maybeTNothing >>
return "second"
maybeTNothing :: MaybeT Identity String
maybeTNothing = MaybeT $ return Nothing
MaybeT
相当于Nothing
的预测似乎是MaybeT $ return Nothing
,感觉有点冗长,我必须明确使用{{1}构造函数。
MaybeT
monad中是否有更短/更好/更清晰的Nothing
写作方式?
答案 0 :(得分:2)
受到@luqui
评论的启发要在普通Nothing
monad中表现为Maybe
,maybeTNothing
应满足等式
maybeTNothing >>= f = maybeTNothing
v >> maybeTNothing = maybeTNothing
这正是来自MonadPlus
的{{3}}的要求,MaybeT
是其中的一个实例。所以我们可以使用mzero
maybeTNothing = mzero