Skip to content

Instantly share code, notes, and snippets.

@MassD
Created May 2, 2015 12:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
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