F# permutations

后端 未结 6 651
予麋鹿
予麋鹿 2020-12-08 17:34

I need to generate permutations on a given list. I managed to do it like this

let rec Permute (final, arr) = 
    if List.length arr > 0 then
        for          


        
6条回答
  •  悲哀的现实
    2020-12-08 18:04

    Here's the solution I gave in my book F# for Scientists (page 166-167):

    let rec distribute e = function
      | [] -> [[e]]
      | x::xs' as xs -> (e::xs)::[for xs in distribute e xs' -> x::xs]
    
    let rec permute = function
      | [] -> [[]]
      | e::xs -> List.collect (distribute e) (permute xs)
    

提交回复
热议问题