Generating permutations in OCaml
Your code is totally imperative. In some cases (probably in most) it's faster, but this not the best way to use OCaml :) Here is my solution in functional style:



Printing the list of lists can be done by iterating over list of lists:

let print lst =
  List.iter (fun l -&gt;
      List.map string_of_int l
      |&gt; String.concat " "
      |&gt; print_endline
    ) lst


Next recursive function does:


Selects head element of the list and makes it heading element of the resulting list
Recursively calls itself on the list of all previous elements (minus resulting subset) + tail.


let rec permutations result other = function
  | [] -&gt; [result]
  | hd :: tl -&gt;
    let r = permutations (hd :: result) [] (other @ tl) in
    if tl &lt;&gt; [] then
      r @ permutations result (hd :: other) tl
    else
      r


All together. Initial result is empty and the stored list of previous elements is also empty:

let () =
  permutations [] [] [1; 2; 3]
  |&gt; print