module S = Set.Make(String)     (* multimi de siruri *)
module StrSet = struct          (* necesar pentru a defini multimi de S.t *)
  type t = S.t                  (* tipul multime de siruri *)
  let compare = compare
end
module SS = Set.Make(StrSet)    (* multimi de multimi de siruri *)

let of_list lst = List.fold_left (fun r e -> S.add e r) S.empty lst

let print_sset s =  (* tipareste o multime de siruri *)
  print_char('{');
  if s <> S.empty then
    let e = S.min_elt s in (
      print_string e;
      S.iter (Printf.printf ", %s") (S.remove e s)
    );
  print_char '}';
  print_newline()

let combset k s =       (* submultimile de k elemente din s *)
  (* rest = de selectat; part = deja selectate; n = |rest| *)
  let rec comb2 n k rest part =         (* multime de submultimi *)
    if k = n then SS.singleton (S.union part rest)      (* ia toate din rest *)
    else if k = 0 then SS.singleton part        (* nu mai trebuie *)
    else let e = S.choose rest in       (* consideram un element *)
         let rst1 = S.remove e rest in  (* restul de selectat *)
         SS.union (comb2 (n-1) (k-1) rst1 (S.add e part))   (* il selectam *)
                  (comb2 (n-1) k rst1 part)     (* nu il selectam *)
  in comb2 (S.cardinal s) k s S.empty   (* initial n = |s|, part = vid *)

(* comparati cu urmatoarea functie, care doar tipareste multimile *)
let printcomb k s =
  let rec pcomb2 n k rest part =         (* multime de submultimi *)
    if k = n then print_sset (S.union part rest)      (* ia toate din rest *)
    else if k = 0 then print_sset part        (* nu mai trebuie *)
    else let e = S.choose rest in       (* consideram un element *)
         let rst1 = S.remove e rest in  (* restul de selectat *)
         (
           pcomb2 (n-1) (k-1) rst1 (S.add e part);   (* il selectam *)
           pcomb2 (n-1) k rst1 part     (* nu il selectam *)
         )
      in pcomb2 (S.cardinal s) k s S.empty   (* initial n = |s|, part = vid *)
;;
SS.iter print_sset (combset 2 (of_list ["1";"2";"3";"4"]))
;;
printcomb 2 (of_list ["1";"2";"3";"4"])

This document was generated using caml2html