OCaml: Set modules

前端 未结 2 735
执念已碎
执念已碎 2021-02-18 19:02

I want to use OCaml to generates sets of data and make comparisons between them. I have seen the documentation for Module types like Set.OrderType, Set.Make

2条回答
  •  梦谈多话
    2021-02-18 19:06

    In addition to Chris's answer, it may be useful to say that some standard library modules already adhere to the OrderedType signature. For example, you can simply do:

    module StringSet = Set.Make(String) ;;       (* sets of strings *)
    module Int64Set = Set.Make(Int64) ;;         (* sets of int64s *)
    module StringSetSet = Set.Make(StringSet) ;; (* sets of sets of strings *)
    

    And so on.

    Here's a simple usage example for StringSet; remember that sets are functional data structures, so adding a new element to a set returns a new set:

    let set = List.fold_right StringSet.add ["foo";"bar";"baz"] StringSet.empty ;;
    StringSet.mem "bar" set ;; (* returns true *)
    StringSet.mem "zzz" set ;; (* returns false *)
    

提交回复
热议问题