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
, etc, but I can't figure out how to initialize a set or otherwise use them.
相关问题
- Writing an interpreter in OCaml [closed]
- installing packages for python 3
- Using Core.Std.List.fold_left without label
- separate styles for separate module in Angular 5
- Drupal 8: How do I customize a form widget to show
相关文章
- C#中 public virtual string Category { get; }这么写会报错:
- How to arrange a Makefile to compile a kernel modu
- php module does not compile. Does not recognize “s
- Sharing an Android library between multiple Androi
- ocaml %identity function
- Functors in Ocaml
- What is the easiest way to add an element to the e
- nodejs modules and duplication? If an app uses two
Sets are defined using a functorial interface. For any given type, you have to create a
Set
module for that type using theSet.Make
functor. An unfortunate oversight of the standard libraries is that they don't defineSet
instances for the built-in types. In most simple cases, it's sufficient to usePervasives.compare
. Here's a definition that works forint
:The module
IntSet
will implement theSet.S
interface. Now you can operate on sets using theIntSet
module:Note that you don't have to explicitly define the input structure for
Set.Make
as anOrderedType
; type inference will do the work for you. Alternatively, you could use the following definition:This has the advantage that you can re-use the same module to instantiate a
Map
:You lose some genericity in using functors, because the type of the elements is fixed. For example, you won't be able to define a function that takes a
Set
of some arbitrary type and performs some operation on it. (Luckily, theSet
module itself declares many useful operations onSet
s.)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: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: