假设我有一个定义为的类型
type value =
None
| Int of int
| Float of float
| Complex of Complex.t
| String of string
| Char of char
| Bool of bool
Run Code Online (Sandbox Code Playgroud)
我希望能够使用Sets这些价值观.根据我的理解,我必须使用仿函数将Set模块具体化为具体类型及其相关排序.
在这个例子中我该怎么做?既然value不能直接在Set.Make仿函数里面使用?
然后我当然需要能够给出这些值的完整排序,所以我应该发明一些东西,比如给不同类型的预定义顺序,然后按它们的有效值排序.我是对的吗?
例如,我可以决定拥有Int of int < Float of int和Int x < Int y是否x < y.这是我实现目标的实用方法吗?
Set.Make仿函数采用带有签名Set.OrderedType的模块:
module type OrderedType = sig type t val compare : t -> t -> int end
Run Code Online (Sandbox Code Playgroud)
Pervasives.compare如果您对订单和min_elt/ 返回的结果没有任何要求,您可以使用max_elt.因此,仿函数的参数可以简单如下:
module T = struct type t = value let compare = compare end
Run Code Online (Sandbox Code Playgroud)