【问题标题】:ML. Error: operator and operand don't agree [tycon mismatch]毫升。错误:运算符和操作数不一致 [tycon mismatch]
【发布时间】:2014-05-24 01:29:23
【问题描述】:

我有这个代码:

datatype ('a, 'b) alterlist = nil | :: of ('a*'b) * ('a, 'b) alterlist; 
infixr 5 :: 

fun build4(x, one, y, two) = (x,one)::(y,two);

我得到这个错误:

datatype ('a,'b) alterlist = :: of ('a * 'b) * ('a,'b) alterlist | nil 
stdIn:41.30-41.46 Error: operator and operand don't agree [tycon mismatch]   
operator domain: ('Z * 'Y) * ('Z,'Y) alterlist   
operand:  ('Z * 'Y) * ('X * 'W)   
in expression:
        (x,one) :: (y,two)

为什么?

【问题讨论】:

    标签: error-handling ml


    【解决方案1】:

    alterlist 的定义中,构造函数:: 将元组作为参数:

    :: of ('a*'b) * ('a, 'b) alterlist
    

    当您使用:: 构造函数构造alterlist 时,您应该使用('a*'b) 类型的一个值和('a, 'b) alterlist 类型的另一个值来调用它。

    您正在尝试使用 (x,one)(y,two) 调用 ::,它们都是一对。解决此问题的一种方法是:

    fun build4(x, one, y, two) = (x,one) :: ((y,two)::nil)
    

    因为((y,two)::nil) 是一个alterlist。

    请注意,:: 的参数类型实际上是 ('a*'b) * ('a, 'b) alterlist,它是一对。 ML 没有函数或构造函数的多个参数的概念,而是传递元组。使用中缀运算符可能会有些混乱,因为编译器会为您做了一些语法糖。

    给定一个函数

    fun f (x, y) = (* ... *)
    

    您可以像f (a, b)f pair 一样正常调用它,其中pair 是一对...如果您声明它是中缀,您可以使用a f b 但这只是用于调用它的语法糖一对。

    【讨论】:

      猜你喜欢
      • 2018-02-11
      • 1970-01-01
      • 2021-05-17
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 2020-05-19
      相关资源
      最近更新 更多