Skip to content

Instantly share code, notes, and snippets.

@zeux
Created February 10, 2012 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeux/1784650 to your computer and use it in GitHub Desktop.
Save zeux/1784650 to your computer and use it in GitHub Desktop.
array permutations
let permute (a: 'a array) =
let swap a i j = Array.permute (fun idx -> if idx = i then j else if idx = j then i else idx) a
let rec p a i = seq {
if i >= Array.length a then yield a
else for j=i to a.Length-1 do yield! p (swap a i j) (i + 1) }
p a 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment