小编Str*_*tr.的帖子

带尾递归的慢字节码

受到这个SO问题答案的启发,我采用代码来检查尾递归的命令循环:

let rec nothingfunc i =
  match i with
  | 1000000000 -> 1
  | _ -> nothingfunc (i+1)

let nothingloop1 () =
  let i = ref 0 in
   while !i < 1000000000 do incr i done;
   1

let timeit f v =
  let t1 = Unix.gettimeofday() in
  let _ = f v in
  let t2 =  Unix.gettimeofday() in
    t2 -. t1

let () =
  Printf.printf "recursive function: %g s\n%!" (timeit nothingfunc 0);
  Printf.printf "while loop with ref counter …
Run Code Online (Sandbox Code Playgroud)

recursion ocaml tail-recursion performance-testing

5
推荐指数
1
解决办法
201
查看次数

单独编译OCaml模块

我已经阅读了这个问题 和其他问题,但我的编译问题尚未解决.

我正在使用这些文件测试单独的编译:

testmoda.ml

module Testmoda = struct
  let greeter () = print_endline "greetings from module a"
end
Run Code Online (Sandbox Code Playgroud)

testmodb.ml

module Testmodb = struct
  let dogreet () = print_endline "Modul B:"; Testmoda.greeter ()
end
Run Code Online (Sandbox Code Playgroud)

testmod.ml

let main () =
  print_endline "Calling modules now...";
  Testmoda.greeter ();
  Testmodb.dogreet (); 
  print_endline "End."
;;
let _ = main ()
Run Code Online (Sandbox Code Playgroud)

现在我生成.mli文件

ocamlc -c -i testmoda.ml >testmoda.mli
Run Code Online (Sandbox Code Playgroud)

和testmoda.cmi在那里.

接下来,我创建.cmo文件没有错误:

ocamlc -c testmoda.ml
Run Code Online (Sandbox Code Playgroud)

很好,所以使用testmodb.ml也一样:

strobel@s131-amd:~/Ocaml/ml/testmod> ocamlc -c -i testmodb.ml >testmodb.mli
File "testmodb.ml", line 3, characters …
Run Code Online (Sandbox Code Playgroud)

ocaml

3
推荐指数
1
解决办法
308
查看次数