module M = Map.Make(String) (* putem crea *orice* dictionar cu cheie sir *)

(* creeaza dictionar din lista de perechi (sir, valoare).
 * Daca un sir apare de mai multe ori se retine ultima valoare *)

let map_of_assoc lst = List.fold_left
                         (fun r (a, b) -> M.add a b r) M.empty lst

let m = map_of_assoc [("x", 5); ("y", 3); ("num", 17); ("x", 7)]

module Int = struct
  type t = int
  let compare = compare
end
module S = Set.Make(Int)   (* acum putem crea multimi de intregi *)

(* creeaza dictionar din lista de perechi (sir, intreg).
 * se retine *multimea* intregilor care apare pentru fiecare sir *)
let setmap_of_assoc lst =
  List.fold_left (fun r (a, b) ->
                  (* s = multimea asociata unui sir a.
                   * daca nu il gasim, dam ca valoare multimea vida *)
                  let s = try M.find a r with Not_found -> S.empty
                  in M.add a (S.add b s) r) M.empty lst
(* adaugam noul sir b la multimea s si stocam rezultatul *)

let m = setmap_of_assoc [("x", 5); ("y", 3); ("num", 17); ("x", 7)]

open Printf
let printmap m = M.iter
                   (fun a b -> printf " %s->" a; 
                               List.iter (printf "%d ") (S.elements b)) m
;;
printmap m

This document was generated using caml2html