F # Recursive member function: "How to define it correctly"

I understand that when you define a recursive member function inside a type, then there is no need to define a function as recursive. The meaning of using a keyword rec.

however, when I do this:

type hello() = class
  member this.recursion(x) =
      match x with
      |10 -> printfn "%A" x
      |_ -> printfn "%A" x
            recursion(x+1)
end

Then I get the error that recursion is not defined.

I tried this.recursion, but then still get a warning:

A recursive reference to the 'this' object is not used. Having a recursive object reference adds runtime initialization checks to members of this and derived types. Try removing this recursive object reference.

So, I am wondering what is the right way to define a recursive member function in a type?

+4
2

, , . , this . :

this.recursion(x+1)

, :

type hello() = class
  member this.recursion(x) =
      match x with
      |10 -> printfn "%A" x
      |_ -> printfn "%A" x
        this.recursion(x+1)
end

, , , :

type hello() = class
  member this.recursion(x) =
    let rec loop x =
      match x with
      |10 -> printfn "%A" x
      |_ -> printfn "%A" x
            loop (x+1)
    loop x
end
+11

( ), , :

type Hello() =

    let rec recursion x =
        match x with
        | 1 -> printfn "%A" x
        | _ -> printfn "%A" x; recursion (x+1)


    member this.Recursion(x) = recursion x
+3

Source: https://habr.com/ru/post/1667363/


All Articles