Skip to content

Instantly share code, notes, and snippets.

@MassD
MassD / permutations_ins_into_all_positions.ml
Last active November 27, 2023 04:10
A implementation for generating all permutations of a list, written in OCaml
(* note that in order to preserve certain order
and also show the conciseness of the implementation,
no tail-recursive is used *)
let ins_all_positions x l =
let rec aux prev acc = function
| [] -> (prev @ [x]) :: acc |> List.rev
| hd::tl as l -> aux (prev @ [hd]) ((prev @ [x] @ l) :: acc) tl
in
aux [] [] l