F# permutations

后端 未结 6 647
予麋鹿
予麋鹿 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:01

    In the spirit of Cyrl's suggestion, here's a sequence comprehension version

    let rec permsOf xs =
      match xs with
      | [] -> List.toSeq([[]])
      | _ -> seq{ for x in xs do
                   for xs' in permsOf (remove x xs) do
                     yield (x::xs')}
    

    where remove is a simple function that removes a given element from a list

    let rec remove x xs =
      match xs with [] -> [] | (x'::xs')-> if x=x' then xs' else x'::(remove x xs')
    

提交回复
热议问题