1

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

这是我的代码。

    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: (_:_:_)
4

1 回答 1

7

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

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

于 2018-10-27T12:01:12.440 回答