Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A implementation for generating permutations using fixed-head approach
let rm x l = List.filter ((<>) x) l
let rec permutations = function
| [] -> []
| x::[] -> [[x]]
| l ->
List.fold_left (fun acc x -> acc @ List.map (fun p -> x::p) (permutations (rm x l))) [] l
(* The List.fold_left makes every element have their oppotunities to be a head *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment