当你从Nothing中抽取时会发生什么?

Ala*_*lan 2 haskell

一般情况下,当我们从画到底发生了什么Nothingdo结构?为了说明我的困惑:为什么会do {x <- Just 1; y <- Nothing; return x}产生Nothing

Lee*_*Lee 9

你的do街区被淘汰了:

Just 1 >>= (\x -> Nothing >>= (\y -> return x))
Run Code Online (Sandbox Code Playgroud)

如果你看一下对于Maybe 的定义(>>=):

(Just x) >>= k      = k x
Nothing  >>= _      = Nothing
Run Code Online (Sandbox Code Playgroud)

你可以看到Nothing >>= (\y -> return x)回报Nothing,那Just 1 >>= (\x -> Nothing)也是Nothing.