I have a node type like the following :
type position = float * float
type node = position
I created those modules for the Map :
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)
I have written this function to add elements to my map.
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
The output was :
Characters 160-186:
Warning 10: this expression should have type unit.
val init_dist : node list -> node -> float DistMap.t = <fun>
I tried this :
let initMap = init_dist nodes (4.67521849144109414,6.85329046476252568);;
However init_dist has type unit, so I was unable to create the Map.
My goal is to be able to use this function to build a map.
The error lays in the code below :
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
Around the 2 pieces of code :
map = DistMap.add...
This is a comparison (and so a boolean) not an assignement as you might wish to implement.
You have to first evaluate map via
let and then process init_dist_aux with new map. Or, since the only difference is the 2nd value (0. or max_float), you can first evaluate this second argument, then process the whole thing as below:
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