Linux 如何在终端命令ocaml中使用模块和脚本?
我正在尝试使用命令Linux 如何在终端命令ocaml中使用模块和脚本?,linux,bash,ocaml,Linux,Bash,Ocaml,我正在尝试使用命令ocaml运行.ml脚本test.ml,并使用我设置的模块template.ml 目前,我知道我可以通过执行ocaml-init template.ml来使用模块运行ocaml,并且可以使用ocaml test.ml运行脚本 我正在尝试运行脚本test.ml,并使用模块template.ml. 我尝试过使用ocaml test.ml,第一行是opentemplate使用ocamlopt-c template.ml编译模板后。在这种情况下,模板未定义。 我还尝试过使用ocaml
ocaml
运行.ml脚本test.ml
,并使用我设置的模块template.ml
目前,我知道我可以通过执行ocaml-init template.ml
来使用模块运行ocaml,并且可以使用ocaml test.ml
运行脚本
我正在尝试运行脚本test.ml,并使用模块template.ml.我尝试过使用
ocaml test.ml
,第一行是opentemplate代码>使用ocamlopt-c template.ml编译模板后。在这种情况下,模板未定义。
我还尝试过使用ocaml-init template.ml test.ml
和open template代码>作为第一行代码。它们分别不工作或出错。
首先,open
命令仅用于控制名称空间。即,它控制一组可见名称。它不具有定位和使模块可访问的效果(通常是假定的)。(通常,您应该避免过度使用open
。这是不必要的;您可以始终使用完整的Module.name
语法。)
ocaml
命令行接受任意数量的已编译ocaml模块,后跟一个ocaml(.ml)文件
因此,您可以通过在开始之前编译template.ml文件来执行您想要的操作:
$ ocamlc -c template.ml
$ ocaml template.cmo test.ml
下面是一个完整的示例,其中包含的文件内容最少:
$ cat template.ml
let f x = x + 5
$ cat test.ml
let main () = Printf.printf "%d\n" (Template.f 14)
let () = main ()
$ ocamlc -c template.ml
$ ocaml template.cmo test.ml
19
值得一提的是,我认为OCaml是一种编译语言,而不是脚本语言。所以我通常编译所有的文件,然后运行它们。使用与上面相同的文件,它如下所示:
$ ocamlc -o test template.ml test.ml
$ ./test
19
我只在希望与解释器交互时使用ocaml
命令(ocaml人员传统上称之为“顶级”)
$ocaml
OCaml版本4.10.0
#设fx=x+5;;
val f:int->int=
#f 14;;
-:int=19
#
首先,open
命令仅用于控制名称空间。即,它控制一组可见名称。它不具有定位和使模块可访问的效果(通常是假定的)。(通常,您应该避免过度使用open
。这是不必要的;您可以始终使用完整的Module.name
语法。)
ocaml
命令行接受任意数量的已编译ocaml模块,后跟一个ocaml(.ml)文件
因此,您可以通过在开始之前编译template.ml文件来执行您想要的操作:
$ ocamlc -c template.ml
$ ocaml template.cmo test.ml
下面是一个完整的示例,其中包含的文件内容最少:
$ cat template.ml
let f x = x + 5
$ cat test.ml
let main () = Printf.printf "%d\n" (Template.f 14)
let () = main ()
$ ocamlc -c template.ml
$ ocaml template.cmo test.ml
19
值得一提的是,我认为OCaml是一种编译语言,而不是脚本语言。所以我通常编译所有的文件,然后运行它们。使用与上面相同的文件,它如下所示:
$ ocamlc -o test template.ml test.ml
$ ./test
19
我只在希望与解释器交互时使用ocaml
命令(ocaml人员传统上称之为“顶级”)
$ocaml
OCaml版本4.10.0
#设fx=x+5;;
val f:int->int=
#f 14;;
-:int=19
#