我正在使用以下功能:
combinations :: Int -> [a] -> [[a]]
combinations k xs = combinations' (length xs) k xs
where combinations' n k' l@(y:ys)
| k' == 0 = [[]]
| k' >= n = [l]
| null l = []
| otherwise = Prelude.map (y :) (combinations' (n - 1) (k' - 1) ys) ++ combinations' (n - 1) k' ys
它适用于我能想到的任何示例,但是当我从较大项目中的其他函数调用它时,在某些情况下我会遇到异常:
Exception: projekt.hs:(34,9)-(38,108): Non-exhaustive patterns in function combinations'
上面的定义有问题吗?它是否缺少某些案例?我认为otherwise
可以处理任何不属于以前案例的事情。