Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top