简短的回答:仅用于平等。
顶级环境中的严格小于运算符 ('a)。
对于整数,使用Int.< 函数,它只接受两个整数作为参数
- Int.<;
val it = fn : int * int -> bool
但是对于相等,情况有点不同,从相等运算符的类型可以看出
- op=;
val it = fn : ''a * ''a -> bool
这里的多态类型是蜜蜂''a,注意双标。这是因为它只能实例化为相等类型(例如,int、string、int'string 等)。请注意,real 不是相等类型!
更新
我通常做的事情是为我创建的每个(数据)类型创建一个比较函数。这样我就可以完全控制发生的事情。比较函数的思想是返回一个order
datatype order = LESS | EQUAL | GREATER
有了这个,您可以轻松地制作一个 case 表达式并做适当的事情,而不是 if .. < .. then .. else ..
更新1
以下代码来自 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