Ocaml 不识别 else

Ocaml does not recognize else

我对 Ocaml 比较陌生,遇到了一个恼人的错误。我的代码在这里:

let rec nfa_to_dfa_step (nfa: ('q,'s) nfa_t) (dfa: ('q list, 's) nfa_t)
    (work: 'q list list) : ('q list, 's) nfa_t =
  match work with 
  | [] -> dfa
  | h::t -> 
    if (h <> []) then 
      (match nfa with { sigma; qs; q0; fs; delta } ->
        (let states = (new_states nfa h) in 
          (union states qs);
          (union states t);
          (union (new_trans nfa h) delta);
          (union (fold_left (fun acc state -> 
            let cur_final = (new_finals nfa state) in
            if (cur_final <> []) then (cur_final::acc) else acc) [] states) fs);
          (nfa_to_dfa_step nfa dfa t)));;
    else 
        (nfa_to_dfa_step nfa dfa t)

我在倒数第二行的 else 语句中遇到语法错误。我相当确定它与括号有关并且需要在没有括号的地方放一些,但我已经在所有内容周围加上括号并且我已经检查并仔细检查了不匹配的括号。如果我在整个 if else 语句周围加上括号,我会得到一个不同的错误:

Error: Syntax error: ')' expected
File "src/nfa.ml", line 90, characters 4-5:
90 |     (if (h <> []) then
         ^
  This '(' might be unmatched

我做错了什么?关于如何调试此类错误的任何想法?当我用 ocaml 编程时,我遇到过很多这样的问题。作为习惯使用命令式低级语言(如 C++)的人,任何有关如何解释和调试此类错误消息的建议都将不胜感激。

如前所述,(nfa_to_dfa_step nfa dfa t))) 之后的 ;; 是罪魁祸首。 更微妙一点, ;; 并非无用/语法无效,它用于结束“顶级短语”。例如:

(* say_hello is at toplevel *)
let say_hello x = 
  (* the body of the function is not *)
  let message = "Hello " ^ x ^ "!" in
  print_endline ()
;; (* here, ;; is a terminaison for the function `say_hello`
      it is perfectly valid *)

为了避免混淆,我们可以在这里和那里读到 ;; 只在 REPL 中有用。在我看来,它对于错误报告很有用。但如果你不想担心,我建议你永远不要把它们放在上面,使用 OCamlformat 让它们为你放置。