在 sml 中输入 int 与 [int ty]

问题描述

我的问题似乎很简单,但我似乎无法在任何地方找到答案。我不确定 sml 中类型 int 和 [int ty] 之间的区别。尝试使用 int 元组列表作为函数输入时出现 [tycon mismatch] 错误。例如:

输入:number_in_month ((1993,2,2),(1776,7 4),(1994,7,5)) 7;

Error: operator and operand don't agree [tycon mistmatch]
operator domain: (int * int * int) list * int
operand:         ([int ty] * [int ty] * [int ty]) *
                 ([int ty] * [int ty] * [int ty]) *
                 ([int ty] * [int ty] * [int ty])

我尝试了几种方法来将我的输入括起来,但似乎没有任何改变结果。

解决方法

SML/NJ(这看起来像是一个错误消息)使用 [int ty](在诊断中)来表示与重载类 Int 相对应的整数类型集。您可以查看 SML 修订后定义 ('97) 的附录 E 了解更多信息,但这与您的问题并不真正相关。

如果你仔细看,你会发现 number_in_month 的域是 (int * int * int) list * int,但是你给它提供了一个 int 三元组---这应该是一个 列表 int 三元组代替。此外,您尝试以柯里化的方式传递另一个参数,但我们可以看到它应该在包含此列表的元组中,而不是基于此错误。

相应地,您可能想要

number_in_month ([(1993,2,2),(1776,7 4),(1994,7,5)],7)

相反。