Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have two methods calling each other?

I'm a bit confused as to how to get two method to call each other (i.e., have A() call B() and B() call A()). It seems that F# only 'sees' the method after it's been encountered in code, so if it hasn't, it just says value or constructor has not been defined.

Am I missing something very basic here?

like image 765
Dmitri Nesteruk Avatar asked Mar 25 '09 07:03

Dmitri Nesteruk


People also ask

Can 2 functions call each other?

Yes two functions can call each other.

Can methods call other methods?

Similarly another method which is Method2() is being defined with 'public' access specifier and 'void' as return type and inside that Method2() the Method1() is called. Hence, this program shows that a method can be called within another method as both of them belong to the same class.

Can you call a method within the same method?

Yes you can. It is called recursion .

Can we call a method twice in Java?

Either you manually call it multiple times by writing a call to the method. You can call the method in a loop and then the method will be called as many times as the loop is executed. You can call the method recursively.


4 Answers

'let rec... and...' is the syntax you seek.

let rec F() = 
    G()
and G() =
    F()

See also Adventures in F# Co-Recursion.

like image 181
Brian Avatar answered Oct 09 '22 13:10

Brian


Since the question is about methods, and Brian's answer is about functions, maybe it's useful to point out that you can use a similar syntax for types:

type A() =
    let b = new B()
    member x.MethodA() = b.MethodB()
and B() =
    member x.MethodB() = ()

Note also that members are 'let rec' by default (in fact I don't think they can be not recursive).

like image 24
Kurt Schelfthout Avatar answered Oct 09 '22 13:10

Kurt Schelfthout


F# 4.1 introduces mutually recursive modules and namespaces.

These are an alternative to the and keyword.

module rec PingPong = // <------ rec keyword here.

    let pong() = 
        printfn "pong"
        ping() 

    let ping () = 
        printfn "ping"
        pong()

The rec keyword defines modules and namespaces that "allow for all contained code to be mutually recursive."

like image 10
Shaun Luttin Avatar answered Oct 09 '22 13:10

Shaun Luttin


Functions declared via let

let rec a () = b ()
and b () = ()

These are mutually recursive functions.

Methods within the same type

type T () =
    member t.A () = t.B()
    member t.B () = ()

This is trivial; it just works. Note Abel's comment though.

Methods within different types

type TypeA () =
    member t.A (b : TypeB) = b.B()

and TypeB () =
    member b.B () = ()

This uses the type ... and syntax for mutually recursive types.

Notes

Normally, and is only used if the calls occur in both directions. Otherwise, it may be better to re-order the declarations so that the called function comes first. It is often helpful for type-inference and readability to avoid circular dependencies, and to not imply them where they aren't used.

I propose to edit the question to either ask for functions in general, or to ask for different types (in which case I would remove the first two cases from this answer). Methods are usually considered to be a subset of functions, which is the general mathematical term. However, all F# functions are technically CLI methods, as that is what they are compiled to. As is, it is not clear what the question is asking for, but I assume from the accepted answer that it does not only ask for methods, as the title would imply.

like image 6
Vandroiy Avatar answered Oct 09 '22 13:10

Vandroiy