Last active
November 22, 2016 18:43
-
-
Save channgo2203/fc0d4453ec1ac99633526cc1225eaa5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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