简短的回答:只是为了平等。
顶级环境中的严格小于运算符 ('a).
对于整数的情况Int.<
使用函数,它只接受两个整数作为参数
- Int.<;
val it = fn : int * int -> bool
然而对于相等,情况有点不同,从相等运算符的类型可以看出
- op=;
val it = fn : ''a * ''a -> bool
这里可以看到多态类型是bee''a
,注意双拼。这是因为它只能实例化为相等类型(例如 int、string、int'string 等)。请注意,real 不是相等类型!
Update
我通常做的事情是为我创建的每个(数据)类型创建一个比较函数。这样我就可以完全控制发生的事情。比较函数的想法是返回一个order http://www.standardml.org/Basis/general.html#SIG%3aGENERAL.order%3aTY
datatype order = LESS | EQUAL | GREATER
有了这个,您可以轻松地创建一个 case 表达式并执行适当的操作,而不是if .. < .. then .. else ..
Update1
下面的代码来自 Andreas Rossberg 的评论。我把它放在这里是为了方便阅读
fun comparePair compareA compareB ((a1, b1), (a2, b2)) =
case compareA (a1, a2) of
EQUAL => compareB (b1, b2)
| other => other
以及一些使用示例
- comparePair Int.compare String.compare ((2, "foo"), (3, "bar"));
val it = LESS : order
- comparePair Int.compare String.compare ((3, "bar"), (3, "bar"));
val it = EQUAL : order