Immutable approach to record composition?

Hi,

I will try to sum up what I have learned about Map in OCaml so far.

The Stdlib.Map module provides the functor (a function over module type) Map.Make. It takes as an argument the module types of the key of your map, for example Map.Make (String) will return the implementation of a map with string keys, but you can use any modules having val compare : t -> t -> int in their signatures. You can define the map with string as key:

module StringMap = Map.Make (String)

or map with a custom type as key:

module CustomMap = Map.Make (struct
   type t = custom_type

   let compare c1 c2 = (* compare c1 to c2 *) 
end)

And such as for list, you can specify the type of the map values:

type map_string_to_int = int StringMap.t

let map : map_string_to_int = StringMap.singleton "key1" 10
1 Like