SML: Error: operator and operand don't agree [tycon mismatch]

SML: Error: operator and operand don't agree [tycon mismatch]

我正在尝试编写一个有两个参数的 SML 函数,第一个是 int,第二个是列表列表。 objective 是将第一个参数插入到第二个参数中每个列表的前面。例如,append_to_front(1,[[3,4],[6,8],[]]) 应该 return [[1,3,4],[1,6,8],[1]].

我有代码:

fun append_to_front(a:int, L:int list list) =
    if L = []
    then []
    else a::hd(L)::append_to_front(a, tl(L));

我收到错误消息:错误:运算符和操作数不一致 [tycon 不匹配]。为什么?

cons运算符::的类型为'a * 'a list -> 'a list,也就是说,它要求左边是一个元素,右边是一个列表。此外,它是右结合的,即 a::b::c = a::(b::c).

在您的例子中,a 的类型为 intbc 的类型均为 int list。因此, :: 的第二次使用类型不正确,因为它的两边都有一个列表。在那个地方使用列表连接 @