(* Filtering, with a predicate, based on all possible comparisions by pair, of the elements from an input list: *) (** filter, with a predicate, from all combinations of 2 elements from n *) let ls = [1; 2; 3; 4; 5; 6] let comb2_filter p ls = let rec aux ls acc = match ls with | [] -> (List.rev acc) | x::xs -> let _acc, ys = List.fold_left (fun (acc, ys) y -> if (p x y) then (acc, y::ys) else (false, ys) ) (true, []) xs in let acc = if _acc then x::acc else acc in let ys = List.rev ys in aux ys acc in aux ls [] let () = let fs = comb2_filter (fun v1 v2 -> match (v1, v2) with | (5, 4) | (4, 5) -> false | _ -> true ) ls in List.iter (fun (v) -> Printf.printf " (%d)\n" v ) fs (* Result: *) (* (1) (2) (3) (6) *)