Sml 标准ML中的结构比较

Sml 标准ML中的结构比较,sml,ml,comparison-operators,Sml,Ml,Comparison Operators,我似乎找不到关于为什么这不起作用的参考: - (2000,1)<(2000,1); stdIn:18.1-18.18 Error: operator and operand don't agree [overload] operator domain: 'Z * 'Z operand: (int * int) * (int * int) in expression: (2000,1) < (2000,1) -(2000,1)简短的回答:只

我似乎找不到关于为什么这不起作用的参考:

- (2000,1)<(2000,1);    
stdIn:18.1-18.18 Error: operator and operand don't agree [overload]
  operator domain: 'Z * 'Z
  operand:         (int * int) * (int * int)
  in expression:
    (2000,1) < (2000,1)

-(2000,1)简短的回答:只为平等


严格小于运算符(感谢您提供的链接,它非常有用。考虑到每个组件本身都是“可比较”的,是否有可能编写一个通用的词典顺序?例如,我的类型是type Date=int*int*int,什么是“继承”词典顺序的最干净的方法?是“一种为平等而硬连接的机制,还是将其推广到“hashable”co“mparable”和其他潜在的用户定义机制?@nicolas:不,相等是唯一具有特殊多态性支持的运算符。要为(比如)对构建字典顺序,您可以轻松编写一个组合器:
fun comparePair compareA compareA compareAB((a1,b1),(a2,b2))=大小写compareA(a1,a2)of EQUAL=>compareAB(b1,b2)| other=>other
并像
comparePair Int.compare String.compare((2,“foo”),(3,“bar”)
一样使用它。这以明显的方式推广到所有其他多态类型(例如三元组、列表、树)或其他操作,如哈希。第14章(内置变量和函数)的提供了如何完成此重载的简短描述。请注意,它特定于MosML,但我希望SML/NJ也会这样做。一个更有趣的示例,展示了如何任意组合:
List.collate(comparePair(comparePair Int.compare Int.compare)String.compare)([…],[…])
——也就是说,该库已经提供了
List.collate
作为列表的通用词典比较函数。
- Int.<;
val it = fn : int * int -> bool
- op=;
val it = fn : ''a * ''a -> bool
datatype order = LESS | EQUAL | GREATER
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