exception Error of (string);;
let v = ref true;;
let e = Error("Fail");;
let h = ref false;;
while !h <> true do
let x = read_int();
let y = read_int();
try
let res = x / y in
Printf.printf "result = %d" res;
h := true;
with
Division_by_zero -> raise e
done;;
在“done ;;”行中返回语法错误,但是我不知道为什么。
最佳答案
let x = read_int();
是顶级定义,而不是表达式。表达式形式为
let x = read_int() in ...
一般来说,您似乎在很大程度上模仿命令式的Algol风格,这只会使您感到困惑,并给您带来很多痛苦。相反,您应该尝试编写没有可变性,分号(在OCaml中是“序列运算符”,而不是行/语句终止符)和命令式迭代的代码。
您可能还会发现像JavaScript一样的ReasonML syntax更熟悉并且更易于处理,但是设置它当然是另一回事,并且必须翻译OCaml学习资源,因此可能不值得额外的麻烦。
关于compiler-errors - 命令代码中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49112800/