Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions that call each other in a module in OCaml

I have a question about ocaml, i'm a beginner :-)

Here is an example of what i'm trying to do : (I know this is non-sense but it's not my real code, it's just an example)

let func a b = a
let func2 a b = b

let func_a a b =
    if b < 0 then
       func_b b a
    else
       func a b

let func_b a b =
    if a < 0 then
       func2 a b
    else
       func_a b a

The problem is: Unbound value func_b in the first "if" in func_a...

If anyone could help?

Edit: I understand why this is unbound, but I dont know how to fix it.

Thanks a lot!

Max

like image 688
DCMaxxx Avatar asked Dec 08 '25 21:12

DCMaxxx


1 Answers

The keyword is mutually recursive functions:

let func a b = a
let func2 a b = b

let rec func_a a b =
    if b < 0 then
       func_b b a
    else
       func a b

and func_b a b =
    if a < 0 then
       func2 a b
    else
       func_a b a
like image 181
pad Avatar answered Dec 11 '25 17:12

pad