您能否提供一个代码片段,展示如何在OCaml中使用Lua?
一个简单的例子可能是"Hello,World"变体.让OCaml提示用户输入名称.然后将该名称传递给Lua函数.让Lua打印问候语并返回名称的长度.然后让OCaml打印一条关于名称长度的消息.
例:
user @ desktop:〜$./ hello.opt
名称?用户
你好,用户.
你的名字长4个字母.
用户@桌面:〜$
[编辑]
作为一名非C程序员,我是否可以实现这一点而无需编写中间C程序来传递Lua和OCaml之间的数据?
以下是我想尝试的理论概念.不幸的是,ocaml_hello.ml的第3行需要知道如何调用lua_hello.lua中定义的函数以使代码有效.
lua_hello.lua 定义lua_hello,它打印一个参数并返回其长度.
1 function lua_hello (name)
2 print ("Hello, "..name..".")
3 return (string.len (name))
4 end
Run Code Online (Sandbox Code Playgroud)
ocaml_hello.ml OCaml提示输入名称,调用Lua函数,并打印返回值.
1 let () = print_string "Name? "; flush stdout in
2 let name = input_line stdin in
3 let len = Lua_hello.lua_hello name in
4 Printf.printf "Your name is %d letters long." len; flush stdout;;
Run Code Online (Sandbox Code Playgroud)