dictionary - 为什么我会得到一个错误类型,其参数为同一目的使用了两次
问题描述
首先,我有这些类型:
type position = float * float
type node = position
type path = position list
以下是导致错误的两段代码:
let build_path map source target =
let rec build_aux acc map source x initial_target =
if (((DistMap.find_opt x map) = None) || x = source) then acc@[initial_target]
else build_aux ((DistMap.find x map)::acc) map source (DistMap.find x map) initial_target
in build_aux [] map source target target
let shortest_path graph source target : path =
build_path (snd (dijkstra graph source target)) source target
path
为了清楚起见,有类型position list
。
这是错误:
361 | build_path (snd (dijkstra graph source target)) source target
^^^^^^
Error: This expression has type position list
but an expression was expected of type position = float * float
我只是不明白。我已经在 Utop 中尝试了 build_path 函数,方法是像这样填充 Map:
DistMap.bindings prevMap;;
- : (node * (float * float)) list =
[((1., 1.), (7., 7.)); ((2., 2.), (1., 1.)); ((3., 3.), (2., 2.));
((4., 4.), (3., 3.)); ((5., 5.), (4., 4.))]
let l = build_list prevMap (1.,1.) (5.,5.);;
val l : node list = [(1., 1.); (2., 2.); (3., 3.); (4., 4.); (5., 5.)]
shortest_path
必须 100% 确定地接收target
type node
。问题是,当 target 用作dijsktra
函数的参数时不会引发错误,这需要 agraph
和两个节点source
and target
。
所以我真的很困惑为什么 target 突然有错误的类型 forbuild_path
和 not dijkstra
。
无论如何要解决这个问题?
解决方案
感谢@Pierre G. 的帮助,我们确定target
type 被函数限制为 a position list
,dijkstra
因为我正在比较一个列表 indijkstra
和 target ,一旦错误被修复并target
与另一个node
in进行比较dijkstra
,问题就解决了。
推荐阅读
- express - SSl 适用于前端,但不适用于后端
- apache-kafka - 将 Kafka Stream 从 2.5.1 升级到 2.6.2 后出现“java.lang.IllegalStateException:试图查找未知任务 3_0 的滞后”
- ruby-on-rails - 为什么这个 where 查询不再适用于 ShopifyAPI?
- javascript - 在反应路由器中分离路由
- javascript - 如何使用 RecordRTC 录制屏幕+音频+麦克风
- html - 在 Tailwind CSS 中,Span 在同一行显示元素
- php - 我想在 Wordpress 中的特定日期之后重定向用户
- javascript - 通过JS将PDF动态图章作为只读对象插入
- c# - 保存使用 LINQ 的 WPF DataGrid 的更改
- javascript - javascript 使用 SMTP 发送电子邮件