Skip to content

Instantly share code, notes, and snippets.

@channgo2203
Last active November 22, 2016 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save channgo2203/fc0d4453ec1ac99633526cc1225eaa5b to your computer and use it in GitHub Desktop.
Save channgo2203/fc0d4453ec1ac99633526cc1225eaa5b to your computer and use it in GitHub Desktop.
let insert_all_positions x l =
let rec aux prev acc l =
match l with
| [] -> (prev @ [x]) :: acc |> List.rev
| hd::tl as l -> aux (prev @ [hd]) ((prev @ [x] @ l) :: acc) tl
in aux [] [] l;;
let rec permutation l =
match l with
| [] -> []
| hd::[] -> [[hd]]
| hd::tl -> List.fold_left (fun acc p -> acc @ insert_all_positions hd p) [] (permutation tl);;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment