我创建了一个包含两个列表的虚拟函数,如下所示:
# let rec test ([a;b]) = match [a;b] with
[] -> []
| h::t ->
if ((List.length h) > 0) then
[List.hd a]
else
[]
;;作为回报,我得到了这个警告:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
[]但是在上面的函数中,我是否在匹配函数的第一次匹配中匹配[]?
这个警告是有意义的,因为当我执行test([]);;时,我会得到一个错误。当我认为我已经在使用上面的代码时,我不知道该由谁来检查这个案例。
发布于 2017-11-01 15:43:37
您在match表达式中的模式是详尽的,实际上它们是非常详尽的,因为模式[]永远无法匹配表达式[a;b]。
并非详尽无遗的是函数签名(([a;b]))中的模式。您应该用一个简单的参数名替换该模式,然后匹配它。所以你可以像这样:
let rec test xs = match xs with
...或者,您也可以只使用function而根本不命名参数:
let rec test = function
...https://stackoverflow.com/questions/47058397
复制相似问题