sml - 为什么标准 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 中出现此错误?谢谢!
解决方案
在 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];
推荐阅读
- google-sheets - Arrayformula 比较两列并跳过空白单元格
- node.js - 如何使用 amqplib 在没有 IP 地址的情况下本地访问 RabbitMQ?
- groovy - 在更新时根据从另一个字段中的选择清除选择自定义字段
- nginx - 如果 nginx.conf 中没有 mime.types 会发生什么?
- vue.js - Vuejs 仅在一个 vue 组件中创建 websocket 连接
- python-3.x - 在 python 轮中仅包含 *.pyc 文件
- azure-devops - Azure DevOps,资源组的默认角色分配是什么?
- ns-3 - 使用 NS3 拓扑生成器时出现问题
- python-3.x - 如何连接matplotlib散点图的某个特定节点?
- java - 选择带有 thymeleaf 和 th:each 的行