可能重复:
Pattern matching variables in a case statement in Haskell
我有以下简化代码(实际代码确实实现了一些有意义的东西 ):
import Data.Char
import Data.Maybe
import Control.Monad
import Control.Applicative
testA :: [Int] -> Maybe Int -> Maybe Int
testA [] _ = Nothing
testA _ Nothing = Nothing
testA (x:xs) v = case x of
1 -> (+) <$> v <*> return x
otherwise -> testA xs v
testB :: (MonadPlus m, Applicative m) => [Int] -> m Int -> m Int
testB [] _ = mzero
testB _ mzero = mzero
testB (x:xs) v = case x of
1 -> (+) <$> v <*> return x
otherwise -> testB xs v
main = do
let xpto = testA [1,2,3,1,5] (Just 5)
print xpto
let ypto = testB [1,2,3,1,5] (Just 5)
print ypto
据我所知,当使用Maybe Monad进行feed时,testA和testB的行为应该相同,这意味着mzero应该转换为Nothing。
然而,在测试B中,ghc实际上发出警告:
Pattern match(es) are overlapped
In an equation for `testB': testB (x : xs) v = ..
输出结果不同:
Just 6 (correct)
Just 5 (incorrect)
我不确定我可能会遗漏什么,为什么mzero在模式匹配中不等同于什么?