我需要一个函数,它能够像Python中的itertools.combinations(iterable, r)
一样执行相同的操作。
目前,我想到了以下代码:
{-| forward application -}
x -: f = f x
infixl 0 -:
{-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -}
combinations :: Ord a => Int -> [a] -> [[a]]
combinations k l = (sequence . replicate k) l -: map sort -: sort -: nub
-: filter (\l -> (length . nub) l == length l)
有没有更优雅和更高效的解决方案?