Skip to content

Instantly share code, notes, and snippets.

Created May 2, 2015 12:45
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 @ (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