compiler-errors - Ocaml - 匹配两个列表
问题描述
我正在尝试在 OCaml 中编写一个 shuffle 函数,但是类型推断存在问题。Merlin 告诉我l1
andl2
是类型'a list list
,这不是真的,因为它们只是'a list
. 为什么它声称?
let shuffle l1 l2 =
let rec scan l1 l2 acc =
match (l1, l2) with
| [],[] -> acc
| ([],h2::t2) -> scan [] t2 h2::acc
| (h1::t1, []) -> scan t1 [] h1::acc
| (h1::t1,h2::t2) -> scan t1 t2 h1::h2::acc
in scan l1 l2 []
;;
解决方案
根本原因是运算符优先级不是由您按空格分组的。也就是说,scan [] t2 h2::acc
被解释为(scan [] t2 h2)::acc
, not scan [] t2 (h2::acc)
,因为函数应用的优先级高于::
。解决方法是在适当的地方添加括号。
有关OCaml 中不同运算符的优先级和关联性,请参阅此表。
推荐阅读
- c++ - ADL 不能在外面工作(即使是结构)
- python - Python 停止生成器中的进程
- amazon-web-services - SNS FIFO 主题不将消息扇出到 SQS FIFO 队列
- node.js - 在 Mikro-ORM 中多次分叉 EntityManager 意味着什么?
- odata - 简单的 OData 客户端 - 在运行时创建 OrderBy 表达式?
- mysql - 如何使用 Rails 中的 update_all() 方法传递可变参数值?
- kubernetes - 字段“resource.subnetwork”的值无效。如果网络资源处于自定义子网模式,网络接口必须指定子网
- r - 从 sliderInput 更新 rhandsontable
- javascript - Godot 无法在 python 烧瓶服务器上加载 .pck 文件;任何人都可以建议修复吗?
- javascript - 我收到错误 app.get is not a function,但其他人可以运行此代码而不会出现任何错误,我想知道为什么会这样?