“模式匹配不详尽”和“此匹配案例未使用”问题

时间:2019-04-21 05:18:15

标签: ocaml

我正在处理分配代码,在我的部分代码中,我遇到了有关OCaml中的模式匹配的问题,我无法弄清楚。

FileManager

我得到的错误是关于这部分的

contentsOfDirectory

我想知道我是否缺少明显的东西

1 个答案:

答案 0 :(得分:1)

您需要添加括号。使用您的代码,OCaml会看到以下内容:

fun frag ->
    match make_and_parser t pf frag with
    | (Some children, suffix) -> match make_or_parser (pf h) pf suffix with
                                 | (None, left) -> (Some children, left)
                                 | (Some tree, left) -> (Some (children @ tree), left)
                                 | (None, suffix) -> match make_or_parser (pf h) pf suffix with
                                                     | (None, left) -> (None, left)
                                                     | (Some tree, left) -> (Some tree, left))

由此可见,第一个模式匹配很不详尽。它缺少(None, _)的模式。由此可见,第二个模式匹配(即(None, suffix))中还有一个未使用的匹配情况。

要解决您的问题,

fun frag ->
    match make_and_parser t pf frag with
    | (Some children, suffix) -> (match make_or_parser (pf h) pf suffix with
                                 | (None, left) -> (Some children, left)
                                 | (Some tree, left) -> (Some (children @ tree), left))
    | (None, suffix) -> (match make_or_parser (pf h) pf suffix with
                        | (None, left) -> (None, left)
                        | (Some tree, left) -> (Some tree, left))

请注意,在模式匹配项周围添加了额外的括号。

最后,您被自己的缩进所迷住了。

相关问题