List all possible 4 chooses from 9 in Haskell

前端 未结 4 1999
温柔的废话
温柔的废话 2021-01-14 10:59

I\'m not able to find an effective way to pick out all permutations of 4 elements from a list of 9 elements in Haskell. The python-way to do the same thing:

         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 11:33

    pick :: Int -> [a] -> [[a]]
    pick 0 _ = [[]]
    pick _ [] = []
    pick n (x : xs) = map (x :) (pick (n - 1) xs) ++ pick n xs
    
    perms :: Int -> [a] -> [[a]]
    perms n l = pick n l >>= permutations
    

提交回复
热议问题