Created
October 1, 2019 08:31
-
-
Save shwars/ccc50a882b5372930629697387310175 to your computer and use it in GitHub Desktop.
Lists permutations
This file contains hidden or 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
let rec ins z = function | |
| [] -> [[z]] | |
| x::xs -> | |
let res = ins z xs | |
let res1 = List.map (fun t -> x::t) res | |
(z::x::xs)::res1 | |
let rec ins z = function | |
| [] -> [[z]] | |
| x::xs as L -> | |
(z::L)::(List.map (fun t -> x::t) (ins z xs)) | |
ins 0 [1;2;3] | |
let rec perm = function | |
| [] -> [[]] | |
| x::xs -> | |
let res = perm xs | |
res |> List.map (fun t -> ins x t) |> List.concat | |
let rec perm = function | |
| [] -> [[]] | |
| x::xs -> | |
perm xs |> List.collect (fun t -> ins x t) | |
perm [1;2;3] | |
let fact n = List.length(perm [1..n]) | |
fact 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment