在 Haskell 中,您通常可以通过内联和术语重写来理解一些代码:
我们有:
Prelude> Just 0 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) )
Nothing
我们需要的最重要的事情是 monad 的定义和定义,如下所示fail
:>>=
Maybe
instance Monad Maybe where
(Just x) >>= k = k x
Nothing >>= _ = Nothing
(Just _) >> k = k
Nothing >> _ = Nothing
return = Just
fail _ = Nothing
所以我们有:
Just 0 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) )
-- by definition of >>=
(\ x -> if (x == 0) then fail "zero" else Just (x + 1) ) 0
-- by definition of fail
(\ x -> if (x == 0) then Nothing else Just (x + 1) ) 0
-- beta reduce
if 0 == 0 then Nothing else Just (0 + 1)
-- Integer math
if True then Nothing else Just 1
-- evaluate `if`
Nothing
你有它。