Python 中的等价物。请注意,这在 CPython 中非常糟糕,因为递归在那里实现得非常糟糕,并且在所有 Python 风格中都非常糟糕,因为列表的构造方式不同(1: [2, 3, 4]) 在 Haskell 中很简单,但在 Python 中做同样的事情需要一些 @ 987654323@ 或丑陋的[1] + [2,3,4] 东西)
from typing import TypeVar, List
A = TypeVar('A')
def combinations(k: int, xs: List[A]) -> List[List[A]]:
def helper(n: int, k2: int, lst: List[A]) -> List[List[A]]:
# base cases
if k2 == 0: return [[]] # if the requested subsequence length is zero
elif k2 >= n: return [lst] # if the requested subsequence length is longer than the whole sequence
elif not lst: return [] # if the sequence itself is empty
y, ys = lst[0], lst[1:]
return [[y] + rest for rest in helper(n-1, k2-1, ys)]
return helper(len(xs), k, xs)
请注意,Haskell 中过度使用守卫让我觉得有些丑陋。我可能会改写为:
combinations :: Int -> [a] -> [[a]]
combinations k xs = combinations' (length xs) k xs
where
combinations' _ 0 _ = [[]]
combinations' _ _ [] = []
combinations' n k' l@(y:ys)
| k' >= n = [l]
| otherwise = map (y :) (combinations' (n - 1) (k' - 1) ys)