Skip to content

Instantly share code, notes, and snippets.

@atsapura
Created March 15, 2021 11:13
Show Gist options
  • Save atsapura/afd47f3860dc14de908cad0125fa3b5c to your computer and use it in GitHub Desktop.
Save atsapura/afd47f3860dc14de908cad0125fa3b5c to your computer and use it in GitHub Desktop.
[<RequireQualifiedAccess>]
module Array =
let private swap (arr: _[]) i j =
let buf = arr.[i]
arr.[i] <- arr.[j]
arr.[j] <- buf
let permutations arr =
match arr with
| null | [||] -> [||]
| arr ->
let last = arr.Length - 1
let arr = Array.copy arr
let rec perm arr k =
let arr = Array.copy arr
[|
if k = last then
yield arr
else
for i in k .. last do
swap arr k i
yield! perm arr (k + 1)
|]
perm arr 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment