Ocaml - 意外输出

Ocaml - unexpected output

我有以下代码:

let str = "AA";;
let i =ref 0;;
let tam_str = String.length str -1;;
let aux_char = 'A';;
let b_aux1 = ref false;;
exception Out_of_loop;;

try
  while !i <= tam_str do
    let c_r = str.[!i] in
    if c_r = aux_char then(
      b_aux1 := true;
      i := !i +1;
    )
    else(
      b_aux1 := false;
      raise Out_of_loop
    )
  done;
with Out_of_loop ->();
if !b_aux1 then
  print_endline "1"
else
  print_endline "0";
;;

我原以为程序会写入字符串“1”,但它返回的是“unit”。但是我不明白为什么...谁能解释一下为什么?

注意各种构造的优先级。你写了

try ...
with Out_of_loop ->
  begin
    ();
    if !b_aux1 then ...
  end

虽然我想你想写

begin
  try ...
  with Out_of_loop -> ()
end;
if !b_aux1 then ...