Skip to content

Instantly share code, notes, and snippets.

@MassD
Created May 2, 2015 12:45
Show Gist options
  • Save MassD/22950955c5efa8f8af88 to your computer and use it in GitHub Desktop.
Save MassD/22950955c5efa8f8af88 to your computer and use it in GitHub Desktop.
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