ocaml_如何使用OCaml作为脚本语言?

ocaml

How to use OCaml as a script language like bash and python? It will be good that I can write a script in OCaml and directly invoke it.

如何使用OCaml作为bash和python等脚本语言? 我可以在OCaml中编写脚本并直接调用它会很好。

For example, I write a script named “script.ml” and then I can invoke it directly by

例如,我编写了一个名为“ script.ml”的脚本,然后可以通过直接调用它

$ ./script.ml

Thanks!

谢谢!

Three helloworld scripts:

三个helloworld脚本:

script3.ml

script3.ml

#! /usr/bin/env ocaml

print_string "Hello world!n";;

script1.ml:

script1.ml:

#! /bin/ocaml

print_string "Hello world!n";;

script2.ml

script2.ml

#! /bin/sh
# (*
exec ocaml "$0" "$@"
*) use "topfind";;

print_string "Hello world!n";;

Idea from findlib quickstart.

来自findlib quickstart的想法。

Remember to make the executable by

记得通过以下方式使可执行文件

chmod 755 script.ml

I prefer script3.ml. The script1.ml is straightforward, but it is bounded to /bin/ocaml for the ocaml command. The script2.ml is more complex, but use your environmental configuration for the location of the ocaml command.

我更喜欢script3.ml。 script1.ml很简单,但是对于ocaml命令,它已绑定到/bin/ocaml 。 script2.ml较为复杂,但是请使用您的环境配置作为ocaml命令的位置。

Also be aware that the overall source code is compiled in total first and then is executed. Hence, no execution happens if there is some error, such as failures to compile the script. This is different from a bash script that will be executed by the interpreter until there is an error happens.

还应注意,总体源代码将首先全部编译,然后再执行。 因此,如果存在某些错误 (例如无法编译脚本),则不会执行任何操作。 这与解释器将在错误发生之前执行的bash脚本不同。

There are other discussions and tools on using OCaml as a scripting language:

关于使用OCaml作为脚本语言还有其他讨论和工具

OCaml as a scripting language.
ocamlscript: natively-compiled OCaml scripts.

OCaml是一种脚本语言
ocamlscript:本机编译的OCaml脚本

Answered by anonymous.
匿名回答。


Most preferred script:

最喜欢的脚本:

$ cat script3.ml 
#! /usr/bin/env ocaml

print_string "Hello world!n";;

翻译自: https://www.systutorials.com/how-to-use-ocaml-as-a-script-language/

ocaml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值