![](/img/trans.png)
[英]Why do i get the TypeError “argument of type 'type' is not iterable”?
[英]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
和兩個節點source
和target
。
所以我真的很困惑為什么 target 突然有錯誤的build_path
類型而不是dijkstra
。
無論如何要解決這個問題?
感謝@Pierre G. 的幫助,我們確定target
類型被dijkstra
函數約束到一個position list
,因為我正在將dijkstra
的列表與目標進行比較,一旦錯誤被修復並且target
與dijkstra
另一個node
進行比較,問題就解決了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.