Skip to content

Instantly share code, notes, and snippets.

@MassD
MassD / permutations_ins_into_all_positions.ml
Last active Aug 29, 2015
A implementation for generating all permutations of a list, written in OCaml
View permutations_ins_into_all_positions.ml
(* 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