首页 > 解决方案 > 为什么标准 ml 中的此功能附加列表不起作用?

问题描述

在 ocaml 中,这可以工作:

# let a b c=b@[c];;
val a : 'a list -> 'a -> 'a list = <fun>
# a [1] 2;;
- : int list = [1; 2]

使用 sml(sml/nj) 时:

- fun a(b,c)=b@[c];;
val a = fn : 'a list * 'a -> 'a list
- a [1] 2;;
stdIn:4.1-4.8 Error: operator and operand do not agree [tycon mismatch]
operator domain: 'Z list * 'Z
operand:         'Y[INT] list
in expression:
a (1 :: nil)

我看到了《ml for the working programming》第 258 页:

fun enq(q,x)=q@[x]

那么为什么在 sml 中出现此错误?谢谢!

标签: sml

解决方案


在 OCaml 示例中,您以柯里化风格编写了函数。这是意料之中的,因为这是该语言的惯用风格。

这种风格在 SML 中有效,但将元组传递给函数更为惯用。你已经a这样定义了。

代码经过轻微编辑以提高可读性。

fun a(b, c) = b @ [c];

但是,在调用它时,您使用的是咖喱风格。

a [1] 2;

在 OCaml 和 SML 之间来回移动时,这是一个可以理解的错误,但它解释了您所看到的问题。

相反,你应该写:

a([1], 2);

或者,您可以a在 SML 中以您在 OCaml 中使用的相同柯里化样式定义:

fun a b c = b @ [c];

推荐阅读