函数在Haskell中生成列表的唯一组合

xzlaal3s  于 2023-06-23  发布在  其他
关注(0)|答案(7)|浏览(176)

有没有Haskell函数可以从列表中生成给定长度的所有唯一组合?

  1. Source = [1,2,3]
  2. uniqueCombos 2 Source = [[1,2],[1,3],[2,3]]

我试着在Hoogle中寻找,但找不到一个功能,这样做了具体。置换不能给予期望的结果。
以前有人用过类似的功能吗?

i7uaboj4

i7uaboj41#

我也不知道一个预定义的函数,但自己写很容易:

  1. -- Every set contains a unique empty subset.
  2. subsets 0 _ = [[]]
  3. -- Empty sets don't have any (non-empty) subsets.
  4. subsets _ [] = []
  5. -- Otherwise we're dealing with non-empty subsets of a non-empty set.
  6. -- If the first element of the set is x, we can get subsets of size n by either:
  7. -- - getting subsets of size n-1 of the remaining set xs and adding x to each of them
  8. -- (those are all subsets containing x), or
  9. -- - getting subsets of size n of the remaining set xs
  10. -- (those are all subsets not containing x)
  11. subsets n (x : xs) = map (x :) (subsets (n - 1) xs) ++ subsets n xs
brgchamk

brgchamk2#

使用Data.List

  1. import Data.List
  2. combinations k ns = filter ((k==).length) $ subsequences ns

参考:99 Haskell Problems
参考文献中有不少有趣的解决方案,我只是挑了一个简洁的。

p4rjhz4m

p4rjhz4m3#

我不清楚你对业绩有多关心。
如果它有任何用处的话,早在2014年,有人发布了某种performance contest的各种Haskell组合生成算法。
26项中的13项组合,执行时间从3秒到167秒不等!最快的入口是由Bergi提供的。下面是一个不明显的(至少对我来说)源代码:

  1. subsequencesOfSize :: Int -> [a] -> [[a]]
  2. subsequencesOfSize n xs = let l = length xs
  3. in if (n > l) then []
  4. else subsequencesBySize xs !! (l-n)
  5. where
  6. subsequencesBySize [] = [[[]]]
  7. subsequencesBySize (x:xs) = let next = subsequencesBySize xs
  8. in zipWith (++)
  9. ([]:next)
  10. ( map (map (x:)) next ++ [[]] )

最近,在从一个大列表(100个中的5个)中挑选几个元素的特定上下文中,问题一直是revisited。在这种情况下,你不能使用像subsequences [1 .. 100]这样的东西,因为它引用的是一个长度为2100 1.26*1030的列表。我提交了一个基于algorithm的状态机,它并不像我所希望的那样具有Haskell的习惯性,但在这种情况下是相当有效的,每个输出项大约30个时钟周期。

旁注:使用 multisets 生成组合?

此外,还有一个Math.Combinatorics.Multiset包可用。这是documentation。我只是简单地测试了它,但它可以用来生成组合。
例如,8个元素中的3个元素的所有组合的集合就像具有两个元素(存在和不存在)的multiset的“排列”,这些元素各自具有3和(8-3)=5的多重性。
让我们使用这个想法来生成8个元素中3个元素的所有组合。有(876)/(321)= 336/6 = 56个。

  1. *L M Mb T MS> import qualified Math.Combinatorics.Multiset as MS
  2. *Math.Combinatorics.Multiset L M Mb T MS> pms = MS.permutations
  3. *Math.Combinatorics.Multiset L M Mb T MS> :set prompt "λ> "
  4. λ>
  5. λ> pms38 = pms $ MS.fromCounts [(True, 3), (False,5)]
  6. λ>
  7. λ> length pms38
  8. 56
  9. λ>
  10. λ> take 3 pms38
  11. [[True,True,True,False,False,False,False,False],[True,True,False,False,False,False,False,True],[True,True,False,False,False,False,True,False]]
  12. λ>
  13. λ> str = "ABCDEFGH"
  14. λ> combis38 = L.map fn pms38 where fn mask = L.map fst $ L.filter snd (zip str mask)
  15. λ>
  16. λ> sort combis38
  17. ["ABC","ABD","ABE","ABF","ABG","ABH","ACD","ACE","ACF","ACG","ACH","ADE","ADF","ADG","ADH","AEF","AEG","AEH","AFG","AFH","AGH","BCD","BCE","BCF","BCG","BCH","BDE","BDF","BDG","BDH","BEF","BEG","BEH","BFG","BFH","BGH","CDE","CDF","CDG","CDH","CEF","CEG","CEH","CFG","CFH","CGH","DEF","DEG","DEH","DFG","DFH","DGH","EFG","EFH","EGH","FGH"]
  18. λ>
  19. λ> length combis38
  20. 56
  21. λ>

因此,至少在功能上,使用多重集来生成组合的想法是可行的。

展开查看全部
disbfnqx

disbfnqx4#

lib中没有这样的操作,但你可以自己轻松实现:

  1. import Data.List
  2. main = putStrLn $ show $ myOp 2 [1, 2, 3]
  3. myOp :: Int -> [a] -> [[a]]
  4. myOp 0 _ = []
  5. myOp 1 l = map (:[]) l
  6. myOp c l = concat $ map f $ tails l
  7. where
  8. f :: [a] -> [[a]]
  9. f [] = []
  10. f (x:xs) = map (x:) $ myOp (c - 1) xs
6rqinv9w

6rqinv9w5#

@melpomene的回答很一般,非常简洁。这可能是你在互联网上看到的许多地方需要combinationsOf函数的情况。
然而隐藏在双重递归的背后,它做了大量不必要的递归调用,这些调用是可以避免的,产生了一个非常高效的代码。也就是说,如果列表的长度短于k,我们不需要进行任何调用。
我建议双重终止检查。

  1. combinationsOf :: Int -> [a] -> [[a]]
  2. combinationsOf k xs = runner n k xs
  3. where
  4. n = length xs
  5. runner :: Int -> Int -> [a] -> [[a]]
  6. runner n' k' xs'@(y:ys) = if k' < n' -- k' < length of the list
  7. then if k' == 1
  8. then map pure xs'
  9. else map (y:) (runner (n'-1) (k'-1) ys) ++ runner (n'-1) k' ys
  10. else pure xs' -- k' == length of the list.
  11. λ> length $ subsets 10 [0..19] -- taken from https://stackoverflow.com/a/52602906/4543207
  12. 184756
  13. (1.32 secs, 615,926,240 bytes)
  14. λ> length $ combinationsOf 10 [0..19]
  15. 184756
  16. (0.45 secs, 326,960,528 bytes)

因此,上面的代码虽然尽可能优化,但仍然效率低下,主要是由于内部的双重递归。作为经验法则,在任何算法中,最好避免双重递归,或者在非常仔细的检查下加以考虑。
另一方面,下面的算法在速度和内存消耗两方面都是完成这项工作的非常有效的方法。

  1. combinationsOf :: Int -> [a] -> [[a]]
  2. combinationsOf k as@(x:xs) | k == 1 = map pure as
  3. | k == l = pure as
  4. | k > l = []
  5. | otherwise = run (l-1) (k-1) as $ combinationsOf (k-1) xs
  6. where
  7. l = length as
  8. run :: Int -> Int -> [a] -> [[a]] -> [[a]]
  9. run n k ys cs | n == k = map (ys ++) cs
  10. | otherwise = map (q:) cs ++ run (n-1) k qs (drop dc cs)
  11. where
  12. (q:qs) = take (n-k+1) ys
  13. dc = product [(n-k+1)..(n-1)] `div` product [1..(k-1)]
  14. λ> length $ combinationsOf 10 [0..19]
  15. 184756
  16. (0.09 secs, 51,126,448 bytes)
展开查看全部
0h4hbjxa

0h4hbjxa6#

Monadic solution for unique combinations

  1. cb _ 0 = [[]]
  2. cb xs n = (nxs >>= (\(nx, x) -> (x:) <$> (cb [z | (n,z) <- nxs, n>nx] (n-1)) )) where nxs = zip [1..] xs
vom3gejh

vom3gejh7#

我也有同样的问题。我试着在标准库中寻找解决方案,但找不到。我想到了这个

  1. genCombinations :: Int -> [a] -> [[a]]
  2. genCombinations 0 _ = [[]]
  3. genCombinations n xs =
  4. genCombinations' [] xs n
  5. where
  6. -- \| Generates new combinations by replacing elements in the old combinations (the accumulator)
  7. -- \| with the first element from the list.
  8. genCombinations' :: [[a]] -> [a] -> Int -> [[a]]
  9. genCombinations' acc [] _ = acc
  10. genCombinations' acc (x : xs) n =
  11. -- replace elements in lists from accumulator, and add a combination made of the first element
  12. let newCombinations = concatMap (replaceElems x) acc ++ [replicate n x]
  13. in newCombinations ++ genCombinations' (acc ++ newCombinations) xs n
  14. replaceElems :: a -> [a] -> [[a]]
  15. replaceElems x xs =
  16. replaceElems' x (length xs) 0 xs
  17. replaceElems' :: a -> Int -> Int -> [a] -> [[a]]
  18. replaceElems' _ _ _ [] = []
  19. -- count - how many elements were replaced before
  20. -- n - total length of the combination
  21. replaceElems' x n count [y]
  22. | count == n - 1 = [[y]] -- all prievous elements were replaced, don't replace now
  23. | count == 0 = [[x]] -- no elements were replaced, replace now
  24. | otherwise = [[x], [y]]
  25. replaceElems' x n count (y : ys) =
  26. -- don't replace the element, don't increment the counter
  27. map (y :) (replaceElems' x n count ys)
  28. -- replace the element, increment the counter
  29. ++ map (x :) (replaceElems' x n (count + 1) ys)

通过在累加器中保存所有生成的组合的列表来工作。当处理输入列表中的下一个元素(x)时,它通过以几乎所有可能的方式用x替换累加器中列表的元素来创建新的组合。然后它添加了一个新的组合,仅由x-s组成。
当替换先前生成的组合中的元素时,它不会替换所有元素,或者一个都不替换,因此不会生成两次组合。
很酷的是,它对输入列表的长度是lazy的,所以它可以生成长度为n的所有组合,即使是从一个无限列表

展开查看全部

相关问题