0

I have two functions :

let fn2 =
  if "something happend" then
    fn1

let rec fn1 =
  if "something" then
    fn2

This is just an example, what I am trying to do. Is there any Idea how to do it?

or should I just send the 2. function to the 1. function as parameter?

Racooon
  • 1,468
  • 2
  • 10
  • 28

2 Answers2

6

You need to use let rec ... and ...:

let rec fn1 x =
    if x = "something" then
        fn2 x
and fn2 x =
    if x = "something else" then
        fn1 x
kvb
  • 54,864
  • 2
  • 91
  • 133
0

You can also nest wrap your functions and create two higher-order-functions that take a function as an argument and apply it.

This could than be sth. like:

let fn2 fn =
  if "something happend" then
    fn fn2

let rec fn1 fn =
  if "something" then
    fn fn1

You can call than call your function like this:

let result = fn1 fn2

If you want your function to be more explicit your can write it for example like:

let rec fn2 (fn:unit->unit) : unit =
  if "something happend" then
    fn fn2

let rec fn1 (fn:unit->unit) : unit =
  if "something" then
    fn fn1

But I think kvb's answer is they better way to go because it is more standard-conform and readable.

Peter Ittner
  • 551
  • 2
  • 13