Set consolidation: Difference between revisions

added ocaml
(added Bracmat)
(added ocaml)
Line 186:
[map[A:true C:true B:true D:true] map[G:true F:true I:true H:true K:true]]
</pre>
 
=={{header|OCaml}}==
 
<lang ocaml>let join a b =
List.fold_left (fun acc v ->
if List.mem v acc then acc else v::acc
) b a
 
let share a b =
let rec aux = function
| x::xs -> List.mem x b || aux xs
| [] -> false
in
aux a
 
let extract p lst =
let rec aux acc = function
| x::xs -> if p x then Some (x, List.rev_append acc xs) else aux (x::acc) xs
| [] -> None
in
aux [] lst
 
let consolidate sets =
let rec aux acc = function
| [] -> List.rev acc
| x::xs ->
match extract (share x) xs with
| Some (y, ys) -> aux acc ((join x y) :: ys)
| None ->
aux (x::acc) xs
in
aux [] sets
 
let print_sets sets =
print_string "{ ";
List.iter (fun set ->
print_string "{";
print_string (String.concat " " set);
print_string "} "
) sets;
print_endline "}"
 
let () =
print_sets (consolidate [["A";"B"]; ["C";"D"]]);
print_sets (consolidate [["A";"B"]; ["B";"C"]]);
print_sets (consolidate [["A";"B"]; ["C";"D"]; ["D";"B"]]);
print_sets (consolidate [["H";"I";"K"]; ["A";"B"]; ["C";"D"]; ["D";"B"];
["F";"G";"H"]]);
;;</lang>
 
=={{header|Python}}==