I'm experimenting with the MaybeT monad, specifically 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
The experession for the MaybeT equivalent of Nothing seems to be MaybeT $ return Nothing, which feels a bit verbose, and it feels unexpected to me to have to explicitly use the MaybeT constructor.
Is there a shorter/nicer/clearer way of writing Nothing in the MaybeT monad?
Inspired by the comment from @luqui
To behave as Nothing in the plain Maybe monad, maybeTNothing should satisfy the equations
maybeTNothing >>= f = maybeTNothing
v >> maybeTNothing = maybeTNothing
This is exactly the requirements on mzero from MonadPlus, which MaybeT is an instance of. So we can just use mzero
maybeTNothing = mzero
https://hackage.haskell.org/package/base-4.9.1.0/docs/Control-Monad.html#t:MonadPlus
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With