When I seemed to understand what return is for in Haskell, I tried to play with different alternatives and it seems that return not only can be used anywhere in the monad chain, but also can be excluded completely
*Main> Just 9 >>= y -> (Just y) >>= x -> return x
Just 9
*Main> Just 9 >>= y -> (return y) >>= x -> (Just y)
Just 9
*Main> Just 9 >>= y -> (Just y) >>= x -> (Just x)
Just 9
Even if I omit return in my own instancing, I only get warning...
data MaybeG a = NothingG | JustG a deriving Show
instance Monad MaybeG where
-- return x = JustG x
NothingG >>= f = NothingG
JustG x >>= f = f x
fail _ = NothingG
Monad.hs:3:10:
Warning: No explicit method nor default method for `return'
In the instance declaration for `Monad MaybeG'
and I still can use the monad
*Main> JustG 9 >>= y -> (JustG 11) >>= x -> (JustG y)
JustG 9
*Main> JustG 9 >>= y -> (NothingG) >>= x -> (JustG y)
NothingG
So what's so special about the return keyword? Is this about more complex cases where I can not omit it? Or because this is the "right" way to do things even if they can be done differently?
UPDATE: .. or another alternative, I could define my own monadic value constructor
finallyMyLastStepG :: Int -> MaybeG Int
finallyMyLastStepG a = JustG a
and produce another variant of the same chain (with the same result)
*Main> JustG 9 >>= y -> (JustG 11) >>= x -> (finallyMyLastStepG y)
JustG 9
See Question&Answers more detail:os