First of all, I have these types :
type position = float * float
type node = position
type path = position list
Here are the two pieces of code causing the error :
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
has type position list
for clarity.
Here's the error :
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
I just don't get it. I've tried the build_path function in Utop, by having a Map filled like this :
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
has to, with 100% certainty, receive target
with type node
. The thing is, no error is raised when target is used as an argument for the dijsktra
function, which requires a graph
and two nodes source
and target
.
So I'm really confused why target suddenly has the wrong type for build_path
and not dijkstra
.
Anyway to resolve this issue?
感谢@Pierre G. 的帮助,我们确定target
类型被dijkstra
函数约束到一个position list
,因为我正在将dijkstra
的列表与目标进行比较,一旦错误被修复并且target
与dijkstra
另一个node
进行比较,问题就解决了。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.