【发布时间】:2014-08-04 15:21:44
【问题描述】:
我的函数 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)
在应用了 -Wall 的 GHCI 中运行函数只会返回
<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 比较陌生,而且很困惑。我认为与[_] [] 和[] [_] 的模式匹配应该可以解决这个问题。
【问题讨论】:
-
尝试用
_替换您的[_]。此外,您可能希望先提供(x:xs) (y:ys)的定义,然后再提供_ _ = True模式。 -
这不会破坏递归吗?每个新的递归都会开始一个新的递归,然后才有机会取消
_ _
标签: haskell