首页 > 解决方案 > 为什么我会得到一个错误类型,其参数为同一目的使用了两次

问题描述

首先,我有这些类型:

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

所以我真的很困惑为什么 target 突然有错误的类型 forbuild_path和 not dijkstra

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

标签: dictionarytypesocaml

解决方案


感谢@Pierre G. 的帮助,我们确定targettype 被函数限制为 a position listdijkstra因为我正在比较一个列表 indijkstra和 target ,一旦错误被修复并target与另一个nodein进行比较dijkstra,问题就解决了。


推荐阅读