F#では、次のようにします。
> let x = Set.empty;;
val x : Set<'a> when 'a : comparison
> Set.add (2,3) x;;
val it : Set<int * int> = set [(2, 3)]
OCamlでは、Baseを使用するときに、比較関数をモジュールに提供する必要があることを理解しています。たとえば、要素タイプが string
let x = Set.empty (module String);;
val x : (string, String.comparator_witness) Set.t = <abstr>
Set.add x "foo";;
- : (string, String.comparator_witness) Set.t = <abstr>
しかし、型の比較関数を持つモジュールを構築する方法がわかりませんint * int
。そのようなモジュールを構築/取得するにはどうすればよいですか?
PPXを使用する場合は、次のことができます。
module IntPair = struct
module T = struct
type t = int * int [@@deriving sexp_of, compare]
end
include T
include Comparable.Make(T)
end
それ以外の場合、完全な実装は次のとおりです。
module IntPair = struct
module T = struct
type t = int * int
let compare x y = Tuple2.compare Int.compare Int.compare
let sexp_of_t = Tuple2.sexp_of_t Int.sexp_of_t Int.sexp_of_t
end
include T
include Comparable.Make(T)
end
次に、このモジュールを使用して空のセットを作成できます。
let int_pair_set = Set.empty (module IntPair)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加