You looked at Functor
, Applicative
, and Monad
, but you may want to check out Alternative
. As an example of its use, Just 3 <|> Nothing
will yield Just 3
and not Nothing
.
For your particular use, if you want a one-liner, you could try:
maybeMin l r = min l r <|> l <|> r
Just to break that down, we first calculate min l r
, which uses the Ord
instance of Maybe
to give the minimum of l
and r
if both are Just
values. If this works, then the computation stops there, but if either one isn't Just
, then we check to see if l
is a Just
value. If it is, then that is the result, and if not, we end up returning r
as the result.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…