函数无法识别模式匹配

时间:2014-08-04 15:21:45

标签: haskell

我的函数paralellenCheck每当我称之为抱怨非详尽模式时就会抛出异常

paralellenCheck定义如下:

paralellenCheck :: (Eq a, Num a ) => [a] -> [a] -> Bool
paralellenCheck [] [] = True
paralellenCheck [_] [] = True
paralellenCheck [] [_] = True
paralellenCheck (x:xs) (y:ys)
  | intervall `elem`  [0,5,7,12]  = False
  | not $ paralellenCheck (x:xs) ys  = False 
  | not $ paralellenCheck xs (y:ys)  = False
  | otherwise  = True
  where intervall = abs (x - y)

在GHCI中使用-Wall运行该函数只返回

<interactive>:10:1:
Warning: Defaulting the following constraint(s) to type `Integer'
           (Eq a0)
             arising from a use of `paralellenCheck' at <interactive>:10:1-15
           (Num a0)
             arising from a use of `paralellenCheck' at <interactive>:10:1-15
In the expression: paralellenCheck [] [2, 3, 4, 5]
In an equation for `it': it = paralellenCheck [] [2, 3, 4, ....]

<interactive>:10:1:
Warning: Defaulting the following constraint(s) to type `Integer'
           (Eq a0)
             arising from a use of `paralellenCheck' at <interactive>:10:1-15
           (Num a0)
             arising from a use of `paralellenCheck' at <interactive>:10:1-15
In the expression: paralellenCheck [] [2, 3, 4, 5]
In an equation for `it': it = paralellenCheck [] [2, 3, 4, ....]
*** Exception: haskelltest.hs:(6,1)-(14,31): Non-exhaustive patterns in function paralellenCheck

我对haskell很新,而且很困惑。我认为与[_] [][] [_]的模式匹配应解决此问题。

1 个答案:

答案 0 :(得分:7)

模式中的

[_]表示“任何一个元素的列表”。你似乎打算“任何列表”。现在,[] (a:b:[])例如不匹配。使用_获取任何列表。

相关问题