使用线程时OCaml语法错误

时间:2018-08-20 14:47:28

标签: syntax-error ocaml

let set n v = Thread.delay 1.0; (n := v) in 

let result1 x = set x (!x + 1) in

let result2 x = set x (!x * !x) in

let run i = 

let original = 2 in

  let r = ref (original) in 

  let t1 = Thread.create result1 r; let v1 = !r in

  let v2 = !(result2 r) ;

  Thread.join t1 ;

  Printf.printf "number is %n" (v1 + v2) in 

let main() =
     run 1;
     run 2 in 

  main() ;;

在上面的程序中,我正在使用线程,但无法弄清楚语法错误在哪里。如果有人可以帮助我找出错误所在。

1 个答案:

答案 0 :(得分:0)

此行:

let t1 = Thread.create result1 r;让v1 =!r in

有两个let,但只有一个in。我认为您还需要另外一个in

作为附带说明,这是一种在文件顶层构建代码的简单方法。具有一系列使用let但不使用in的顶级定义会更常见。所以看起来像这样:

let set n v = Thread.delay 1.0; (n := v)
let result1 x = set x (!x + 1)
let result2 x = set x (!x * !x)

let run i =
    let original = 2 in
    let r = ref (original) in
    let t1 = Thread.create result1 r in
    let v1 = !r in
    let v2 = !(result2 r) in
    Thread.join t1;
    Printf.printf "number is %n" (v1 + v2)

let main () = run 1; run 2

let () = main () 

(我尚未编译此文件,因此可能会出现一些语法错误,请提前道歉。仅尝试显示基本布局。)