简体   繁体   English

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

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

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.为了清楚起见, path具有类型position list

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 :我已经在 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 has to, with 100% certainty, receive target with type node . shortest_path必须 100% 确定地接收类型为node target 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 .问题是,当 target 用作dijsktra函数的参数时不会引发错误,这需要一个graph和两个节点sourcetarget

So I'm really confused why target suddenly has the wrong type for build_path and not dijkstra .所以我真的很困惑为什么 target 突然有错误的build_path类型而不是dijkstra

Anyway to resolve this issue?无论如何要解决这个问题?

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

暂无
暂无

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

相关问题 为什么我收到TypeError“类型'类型'的参数不可迭代”? - Why do i get the TypeError “argument of type 'type' is not iterable”? 为什么递归清理 json object 时出现“不可散列的类型:dict”错误? - Why do I get 'unhashable type: dict' error when recursively cleaning json object? 为什么在尝试修改字典时出现此错误:“ TypeError:不可哈希类型:'sl​​ice' - Why do I get this error while trying to modify a dictionary: "TypeError: unhashable type: 'slice' 为什么我将映射的类型不匹配作为路由参数? - Why do I get a type mismatch for a map as a routing parameter? 迭代 python 字典的键,当键是整数时,我收到此错误,“TypeError:'int' 类型的参数不可迭代” - Iterating over the keys of a python dictionary, when keys are integers, I get this error, "TypeError: argument of type 'int' is not iterable" 当一切似乎都很好时,为什么我会收到语法错误? - Why do I get a syntax error when everything seems to be fine? 使用 concat 作为字典我收到错误:第一个参数必须是 pandas 对象的可迭代对象,您传递了“DataFrame”类型的 object - Using concat for a dictionary I get the error: first argument must be an iterable of pandas objects, you passed an object of type “DataFrame” 为什么会出现IndexError? - Why do I get an IndexError? 为什么在以下代码中无法投射类型为'WhereSelectEnumerableIterator的对象? - Why do I get a Unable to cast object of type 'WhereSelectEnumerableIterator for the following code? 如何从Clojure中的map宏获取宏中的参数? - How do I get the argument in a macro from a map macro in Clojure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM