Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 527

How to build a Map using a list?

$
0
0

I have a node type like the following :

type position = float * floattype 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  endmodule 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.


Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>