OCaml:在定义函数之前声明它

Cas*_*ton 10 ocaml functional-programming function mutual-recursion function-declaration

有没有办法在OCaml中定义函数之前声明一个函数?我正在使用OCaml解释器.

我有两个功能:

let myFunctionA = 
(* some stuff here..... *) myFunctionB (*some stuff *)

let myFunctionB = 
(* some stuff here .... *) myFunctionA (* some stuff *)
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为myFunctionA在制作之前无法调用myFunctionB.

我做了一些谷歌搜索,但似乎找不到任何东西.我怎么能做到这一点?

Mar*_*bon 22

你想要的是使这两个函数相互递归.而不是使用"let ... let ...",你必须使用"let rec ... and ...",如下所示:

let rec myFunctionA = 
(* some stuff here..... *) myFunctionB (*some stuff *)

and myFunctionB = 
(* some stuff here .... *) myFunctionA (* some stuff *)
Run Code Online (Sandbox Code Playgroud)