组合生成

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

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"]

by 1957
原文地址:https://www.cnblogs.com/x1957/p/2941637.html