我想问一下,如果一个程序是用Ocaml编写的,能否称其为用Python编写的程序?如果答案是肯定的,那么我该怎么做呢?
let ls = Unix.open_process_in "ls"
try
while true do
Printf.printf "%s\n" (input_line ls)
done
with End_of_file -> ()
Unix.close_process_in ls
let program = "path_to_python_program_exe" in
match Unix.fork () with
| 0 -> (try
Unix.execvp program [|program; "any_more_args_here"|]
with
_ -> printf "%s" "error while execv\n"; exit (-1))
| -1 -> printf "%s" "error accured on fork\n"
| _ -> ignore (wait ()); printf "%s" "parent exit...\n"
当你编译时,需要使用unix.cma作为编译器:ocamlc unix.cma you_ml.ml
我没有实际的经验,但这听起来很有趣(来自将Python与其他语言集成的维基):
Pycaml:使用OCaml编写Python扩展模块(而不是C),并从OCaml程序中使用Python代码和本地库。
Sys.command
执行命令,所以您只需执行 Sys.command "python foo.py"
,假设 python 在您的路径中并且 foo.py 在当前目录中。这取决于您的具体要求,但是您可以使用Python的os.system()函数以与从命令行调用程序相同的方式执行程序。这应该是一个很好的起点。
os.system()
,因为subprocess
模块更可取。 - nosklo