图案不匹配:(_:_:_)



所以我试图构建一个函数,该函数接受元组列表,并找到第二个元素最大的元组。但我遇到了一个模式匹配错误。

这是我的密码。

resultTuple :: [((Int,Int),Int)] -> (Int,Int)
resultTuple [] = error "something wrong"
resultTuple [x] = fst(x)
resultTuple (x:y:xs)
| snd(x) >= snd(y) = resultTuple(x:xs)
| snd(x) < snd(y) = resultTuple(y:xs)

这是我的错误。

Pattern match(es) are non-exhaustive
In an equation for ‘resultTuple’: Patterns not matched: (_:_:_)

x:y:xs的所有情况都有一个条件,编译器警告您没有涵盖所有条件都为false的情况。也就是说,编译器警告snd x >= snd ysnd x < snd y都为false的情况。

当然,这实际上不可能发生,但编译器没有意识到这一点。要消除警告,只需将第二个条件替换为otherwise即可。

最新更新