let rec permutations xs = match xs with | [] -> [[]] | _ -> List.fold_left (fun acc x -> let rest = List.filter (fun y -> y <> x) xs in let subs = permutations rest in acc @ List.map (fun p -> x :: p) subs ) [] xs ;; let ps = permutations [1; 2; 3; 4] in let count = ref 0 in List.iter (fun p -> match p with | [a; _; _; b] when a < b -> count := !count + 1 | _ -> () ) ps; !count