在本教程中,我发现了以下代码段:
deposit :: (Num a) => a -> a -> Maybe a
deposit value account = Just (account + value)
withdraw :: (Num a,Ord a) => a -> a -> Maybe a
withdraw value account = if (account < value)
then Nothing
else Just (account - value)
eligible :: (Num a, Ord a) => a -> Maybe Bool
eligible account =
deposit 100 account >>=
withdraw 200 >>=
deposit 100 >>=
withdraw 300 >>=
deposit 1000 >>
return True
main = do
print $ eligible 300 -- Just True
print $ eligible 299 -- Nothing
我无法弄清楚该>>=
功能应该如何工作。起初它接受一个Maybe a
值作为它的第一个参数:deposit 100 account >>=
之后,它似乎将a -> Maybe a
其作为第一个参数:withdraw 200 >>=
编译器如何批准?不应该>>=
总是Maybe a
作为它的第一个参数吗?
一个可能的解决方案是,如果>>=
函数的优先级按以下方式工作:((a >>= b) >>= c) >>= d
但据我所知,情况恰恰相反:a >>= (b >>= (c >>= d))