我有一个如下所示的节点类型:
type position = float * float
type node = position
我为地图创建了这些模块:
module MyMap =
struct
type t = node
let compare (a1,b1) (a2,b2) =
if a1 > a2 then 1
else if a1 < a2 then -1
else if b1 > b2 then 1
else if b1 < b2 then -1
else 0
end
module DistMap = Map.Make(MyMap)
我编写了这个函数来向我的地图添加元素。
let init_dist nodes source =
let testMap = DistMap.empty in
let rec init_dist_aux nodes map source =
match nodes with
| [] -> map
| x::tl -> if x = source then map = DistMap.add x 0. map else map = DistMap.add x max_float map;
init_dist_aux tl map source
in init_dist_aux nodes testMap source
输出为:
Characters 160-186:
Warning 10: this expression should have type unit.
val init_dist : node list -> node -> float DistMap.t = <fun>
我试过这个:
let initMap = init_dist nodes (4.67521849144109414,6.85329046476252568);;
但是init_dist有类型单位,所以我无法创建地图。
我的目标是能够使用这个函数来构建地图。
错误在于下面的代码:
match nodes with
| [] -> map
| x::tl -> if x = source then map = DistMap.add x 0. map else map = DistMap.add x max_float map;
init_dist_aux tl map source
围绕 2 段代码:
map = DistMap.add...
这是一个比较(因此是一个布尔值(,而不是您可能希望实现的赋值。 您必须首先评估map via
let,然后使用新地图处理init_dist_aux。或者,由于唯一的区别是第二个值(0. 或 max_float(,您可以先计算第二个参数,然后按如下方式处理整个参数:
match nodes with
| [] -> map
| x::tl -> let v = if (x = source)
then 0.
else max_float
in
init_dist_aux tl (Dist.add x v map) source