如何检测 Haskell 中列表的结尾?

How to detect the end of a list in Haskell?

我正在编写一个使用特定公式计算 2 个列表的递归函数。 但我会简化功能,以便您理解我遇到的问题,因为这里的重点是检测列表的 []。

所以我有以下代码:

listSum::([Integer],[Integer])->Double
listSum ((x:xs),(y:ys)) 
    | ((x:xs),(y:ys))==(x:[],y:[])=0.0
    | otherwise = (((fromIntegral x)::Double)+((fromIntegral y)::Double)) + listSum ((xs),(ys))

我现在的输出

listSum([1,2],[1,2])
2.0

listSum([1],[1])
0.0

listSum([],[])
*** Exception: file.hs: .....: Non-exhaustive patterns in function ListSum

以及我希望得到的输出

listSum([1,2],[1,2])
6.0

listSum([1],[1])
2.0

listSum([],[])
0.0

我错过了什么?还是我写的太多了?

您的函数中不需要第一个守卫。你可以简单地写成下面这样(我只是删除了 ::Double 因为 Haskell 可以推断它)

listSum :: ([Integer], [Integer]) -> Double
listSum ([], []) = 0.0
listSum ((x:xs),(y:ys)) = fromIntegral x + fromIntegral y + listSum (xs, ys)

现在,只要传递给 listSum 的参数是空列表,结果将是 0.0,否则将调用递归函数。

注意:只有当两个列表的大小相等时,以上函数才会起作用。否则,你需要这样写

listSum::([Integer],[Integer])->Double
listSum ([], []) = 0.0
listSum ((x:xs), []) = fromIntegral x + listSum(xs, [])
listSum ([], (y:ys)) = fromIntegral y + listSum(ys, [])
listSum ((x:xs),(y:ys)) = fromIntegral x + fromIntegral y + listSum (xs, ys)

注:更简单,整个代码可以这么写,,像这样

pairwiseSum xs ys = sum (zipWith (+) xs ys)