Wie schreibe ich Code in F # für das, was Funktoren in OCaml tun?

Ich habe viele Programme in OCaml geschrieben, einige von ihnen verwenden Funktoren. Jetzt überlege ich, einen Teil des Codes in F # zu schreiben und neu zu schreiben (um einige Vorteile zu nutzen, die OCaml nicht hat). Eine Sache, vor der ich Angst habe, ist, Code in F # für das zu schreiben, was Funktoren in OCaml tun.

Zum Beispiel, wie könnten wir @ emulierdieses Beispiel aus dem OCaml-Handbuch in F #?

type comparison = Less | Equal | Greater

module type ORDERED_TYPE = sig
  type t
  val compare: t -> t -> comparison
end

module Set =
functor (Elt: ORDERED_TYPE) -> struct
    type element = Elt.t
    type set = element list
    let empty = []
    let rec add x s =
      match s with
        [] -> [x]
      | hd::tl ->
         match Elt.compare x hd with
           Equal   -> s         (* x is already in s *)
         | Less    -> x :: s    (* x is smaller than all elements of s *)
         | Greater -> hd :: add x tl
  end

module OrderedString = struct
  type t = string
  let compare x y = if x = y then Equal else if x < y then Less else Greater
end

module OrderedInt = struct
  type t = int
  let compare x y = if x = y then Equal else if x < y then Less else Greater
end

module StringSet = Set(OrderedString)
module IntSet = Set(OrderedInt)

let try1 () = StringSet.add "foo" StringSet.empty
let try2 () = IntSet.add 2 IntSet.empty

Antworten auf die Frage(6)

Ihre Antwort auf die Frage