我正在尝试翻译以下 Haskell 代码:
-- sublistSums list sum returns a list of the sublists of list that add up to sum
sublistSums :: [Int] -> Int -> [[Int]]
sublistSums [] 0 = [[]]
sublistSums [] _ = []
sublistSums (x:xs) sum = [x:tailList | tailList <- sublistSums xs (sum-x)] ++
sublistSums xs sum
...进入序言。该代码应该给我一个总和等于目标数字的所有列表的列表。例如,
?- sublistSums([[1,2,3],[4,5,6]],6).
[[1,2,3]]
如果它绑定应该给你,因为1+2+3=6.
这是我到目前为止所拥有的:
sublistSums([], 0) :-
sublistSums([],0,[]).
sublistSums([], _) :-
sublistSums([],0,[]).
sublistSums([x|xs], sum) :-
conc(conc(x,findall(xs,(sublistSums(xs, (sum-x))),List)), sublistSums(xs, sum)).
但是,当我调用 sublistSums 函数时,它给了我这个:
?- sublistSums([[1,2,3],[4,5,6]],6).
false.
我也试过:
sublistSums([], 0, ReList) :-
[[]].
sublistSums([], _, ReList) :-
[].
sublistSums([x|xs], sum, ReList) :-
conc(conc(x,findall(xs,(sublistSums(xs, (sum-x)),ReList),List)), sublistSums(xs, sum, ReList)).
仍然给我同样的结果。不完全确定我在这里做错了什么。