Skip to content

Instantly share code, notes, and snippets.

@shwars
Created October 1, 2019 08:31
Show Gist options
  • Save shwars/ccc50a882b5372930629697387310175 to your computer and use it in GitHub Desktop.
Save shwars/ccc50a882b5372930629697387310175 to your computer and use it in GitHub Desktop.
Lists permutations
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