繁体   English   中英

为什么我会得到一个错误类型,其中的参数被用于同一目的两次

[英]Why do I get an error type with an argument that is used twice for the same purpose

首先,我有这些类型:

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 函数,方法是像这样填充地图:

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% 确定地接收类型为node target 问题是,当 target 用作dijsktra函数的参数时不会引发错误,这需要一个graph和两个节点sourcetarget

所以我真的很困惑为什么 target 突然有错误的build_path类型而不是dijkstra

无论如何要解决这个问题?

感谢@Pierre G. 的帮助,我们确定target类型被dijkstra函数约束到一个position list ,因为我正在将dijkstra的列表与目标进行比较,一旦错误被修复并且targetdijkstra另一个node进行比较,问题就解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM