List 如何使用列表构建地图?
我的节点类型如下所示:List 如何使用列表构建地图?,list,dictionary,ocaml,List,Dictionary,Ocaml,我的节点类型如下所示: 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
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)
结果是:
Characters 160-186:
Warning 10: this expression should have type unit.
val init_dist : node list -> node -> float DistMap.t = <fun>
但是init_dist有类型unit,所以我无法创建映射
我的目标是能够使用此函数构建地图。错误在于以下代码:
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
围绕两段代码:
map = DistMap.add...
这是一个比较(因此是一个布尔值),而不是您可能希望实现的赋值。
您必须首先通过let评估map,然后使用新map处理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
错误出现在下面的代码中:
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
围绕两段代码:
map = DistMap.add...
这是一个比较(因此是一个布尔值),而不是您可能希望实现的赋值。
您必须首先通过
let评估map,然后使用新map处理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
考查听写理解考查听写理解