Created
March 15, 2021 11:13
-
-
Save atsapura/afd47f3860dc14de908cad0125fa3b5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[<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