F# permutations

后端 未结 6 652
予麋鹿
予麋鹿 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 17:52

    IMHO the best solution should alleviate the fact that F# is a functional language so imho the solution should be as close to the definition of what we mean as permutation there as possible. So the permutation is such an instance of list of things where the head of the list is somehow added to the permutation of the rest of the input list. The erlang solution shows that in a pretty way:

    permutations([]) -> [[]];
    permutations(L) -> [[H|T] H<- L, T <- permutations( L--[H] ) ].
    

    taken fron the "programming erlang" book

    There is a list comprehension operator used, in solution mentioned here by the fellow stackoverflowers there is a helper function which does the similar job basically I'd vote for the solution without any visible loops etc, just pure function definition

提交回复
热议问题