我需要在 haskell 中创建一个函数,它接收一个数字并返回一个列表列表,其中列表包含所有数字的组合,其总和是收到的数字。
很难解释,所以这是一个例子:
sum1 4 = [ [4], [3,1], [2,2], [1,3], [1,1,2], [1,2,1] , [2,1,1] ]
sum1 3 = [ [3], [2,1], [1,2], [1,1,1]
Run Code Online (Sandbox Code Playgroud)
我需要用递归和理解列表来做到这一点
编辑
这是我的代码:
sum1 n = sum3 (sum2 1 (n-1) n)
sum2 x y n = if ((x+y)==n && x>0 && y>0) then [x,y]:sum2 (x+1) (y-1) n else []
sum3 [] = []
sum3 (x:xs) = sum4 x 1 : sum3 xs
sum4 [] t = []
sum4 (x:xs) t = if not (x == …Run Code Online (Sandbox Code Playgroud)