문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top