调用OCaml中其他文件中的函数 [英] Calling functions in other files in OCaml
问题描述
我有具有长度函数的hello.ml:
I have hello.ml that has a length function:
let rec length l =
match l with
[] -> 0
| h::t -> 1 + length t ;;
使用功能的
call.ml:
call.ml that uses the function:
#use "hello.ml" ;;
print_int (length [1;2;4;5;6;7]) ;;
在解释器模式(ocaml)中,我可以使用ocaml call.ml
来获取结果,但是当我尝试使用ocamlc或ocamlbuild对其进行编译时,出现了编译错误.
In interpreter mode (ocaml), I can use ocaml call.ml
to get the results, but when I tried to compile it with ocamlc or ocamlbuild, I got compilation error.
File "call.ml", line 1, characters 0-1:
Error: Syntax error
然后,如何修改调用方,被调用方和构建命令以将代码编译为可执行文件?
Then, how to modify the caller, callee, and build command to compile the code into executables?
推荐答案
#use
指令仅在顶级(解释器)中有效.在已编译的代码中,您应该使用模块名称:Hello.length
.
The #use
directive only works in the toplevel (the interpreter). In compiled code you should use the module name: Hello.length
.
我将展示如何从类似Unix的命令行构建程序.您必须将其适应您的环境:
I'll show how to build the program from a Unix-like command line. You'll have to adapt this to your environment:
$ ocamlc -o call hello.ml call.ml
$ ./call
6
这篇关于调用OCaml中其他文件中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!