Add recursive function to class as member

I'm having trouble adding a recursive function to the class.

I can declare a private function without any problems on my member declarations using let.

But when I try to make it publicly available with a member, it does not compile.

member this.rec mux xs ys =
  match xs with
  | [] -> ys
  | x::xt -> x :: mux ys xt

Thank you for correcting me and pointing to the correct resource on the Internet. I read a lot of tutorials, but I can not find this information.

+4
source share
1 answer

Member functions are always recursive; no keyword is required rec:

member this.mux xs ys =

(and even if there is a keyword rec, it will go to this, just like private- member rec this.mux ...)

, , .. this.mux mux:

member this.mux xs ys =
  match xs with
  | [] -> ys
  | x::xt -> x :: this.mux ys xt

- ,

( )

let - . :

let f x = x+5
let f x = x-2
let a = f 5   // a = 3, not 10

( ) , . :

let sendEmail email subject body =
   let email = canonicalize email
   ...

, , , - , (: "" email, , ).

email email. "".

, email email. , email : , email email , , .

" " - - " email ? " " ?". . , . :

let notifyUsers sendEmail log =
   let sendEmail name =
      log ("Notifying " + name)
      sendEmail (name + "@contoso.com")

   sendEmail "John"
   sendEmail "Mark"
   sendEmail "Matthew"
   sendEmail "Luke"

"" sendEmail , sendEmail. "" sendEmail (.. let rec sendEmail name = ...), : . , .

- : , .

- .
, : ... , - .
, Haskell, , . , .

+7

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


All Articles