就是每个元素取还是不取!

 

combinations :: Int -> [a] -> [[a]]
combinations _ [] = [[]]
combinations 0 _  = [[]]
combinations k (x:xs) = x_start ++ others
   where
       x_start = [x:rest | rest <- combinations (k - 1) xs]
       others = if k <= length xs then combinations k xs else []

*Main> combinations 3 "abcedfg"
["abc","abe","abd","abf","abg","ace","acd","acf","acg","aed","aef","aeg","adf","
adg","afg","bce","bcd","bcf","bcg","bed","bef","beg","bdf","bdg","bfg","ced","ce
f","ceg","cdf","cdg","cfg","edf","edg","efg","dfg"]

 

相关文章:

  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-28
  • 2021-05-22
  • 2022-12-23
  • 2021-06-19
  • 2021-05-28
猜你喜欢
  • 2021-12-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
相关资源
相似解决方案