模組 Set

module Set: sig .. end

基於排序型別的集合。

這個模組實作了集合資料結構,並給定一個集合元素上的全序關係函式。所有集合的操作都是純粹應用式的(沒有副作用)。實作採用了平衡二元樹,因此效率相當高:例如,插入和成員檢查的時間複雜度與集合的大小呈對數關係。

Set.Make 函子 (functor) 根據 compare 函式,為任何型別建構實作。例如:

     module IntPairs =
       struct
         type t = int * int
         let compare (x0,y0) (x1,y1) =
           match Stdlib.compare x0 x1 with
               0 -> Stdlib.compare y0 y1
             | c -> c
       end

     module PairsSet = Set.Make(IntPairs)

     let m = PairsSet.(empty |> add (2,3) |> add (5,7) |> add (11,13))
   

這會建立一個新的模組 PairsSet,其中包含一個新的型別 PairsSet.t,代表 int * int 的集合。


module type OrderedType = sig .. end

Set.Make 函子的輸入簽名 (input signature)。

module type S = sig .. end

Set.Make 函子的輸出簽名 (output signature)。

module Make: 
functor (Ord : OrderedType-> S with type elt = Ord.t

函子根據完全排序型別建構集合結構的實作。