Regarding monads:
Yes, Either a
is a monad. So simplifying the problem, you are basically asking for this:
main = print $ magicMonadUnwrap v
v :: Either String Int
v = Right 3
magicMonadUnwrap :: (Monad m) => m a -> a
magicMonadUnwrap = undefined
How do you define magicMonadUnwrap
? Well, you see, it's different for each monad. Each one needs its own unwrapper. Many of these have the word "run" in them, for example, runST
, runCont
, or runEval
. However, for some monads, it might not be safe to unwrap them (hence the need for differing unwrappers).
One implementation for lists would be head
. But what if the list is empty? An unwrapper for Maybe
is fromJust
, but what if it's Nothing
?
Similarly, the unwrapper for the Either
monad would be something like:
fromRight :: Either a b -> b
fromRight (Right x) = x
But this unwrapper isn't safe: what if you had a Left
value instead? (Left usually represents an error state, in your case, a parse error). So the best way to act upon an Either
value it is to use the either
function, or else use a case statement matching Right
and Left
, as Daniel Wagner illustrated.
tl;dr: there is no magicMonadUnwrap
. If you're inside that same monad, you can use <-
, but to truly extract the value from a monad...well...how you do it depends on which monad you're dealing with.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…