嗯,Maybe
的MonadPlus
实例
instance MonadPlus Maybe where
mempty = Nothing
和guard
作为
guard b = if b then return() else mempty
-- = if b then Just () else Nothing
知道这一点实现,你可以用等式推理来推断,当m
为Maybe
,你可代替原代码
monadPlusSDif x y = guard (y /= 0) >> return (div x y)
与
monadPlusSDif x y = (if y /= 0
then Just()
else Nothing) >> Just (div x y)
或
monadPlusSDif x y
| y /= 0 = Just() >>= \_ -> Just (div x y)
| otherwise = Nothing >>= \_ -> Just (div x y)
或
monadPlusSDif x y
| y /= 0 = Just (div x y)
| otherwise = Nothing
或
monadPlusSDif x y
| y == 0 = Nothing
| otherwise = Just (div x y)
,所以你看到的功能是相同的。
将来,请花时间将代码输入到Stackoverflow编辑器中,代码图片不会考虑其他想要帮助的人。 – bheklilr