Prolog "findall/3" 没有 return 列表

Prolog "findall/3" does not return a list

我很难理解为什么 findall/3 在我的程序中会这样。基本上我写了 I predicate , 应该取一个列表,并且 return 每个可能的索引对,例如,如果我的列表长度为 3, pairs([1,2,3],L) 将 return [1/2, 1/3, 2/3] 这是我预期的列表。(在我的 pairs 谓词中,我用 findall 生成了这个列表 L)
但我已经写了另一个谓词,它应该 return 我一个列表的所有子列表,一个列表和谓词也使用 findall,但结果不是我所期望的。

/*calculate a list CASES with all the sublists from LIST .*/
test(LIST, CASES):-
   pairs(LIST, L),
   member(X/Y, L),  
   findall(SUBLIST, sublist(LIST, X, Y,  SUBLIST), CASES).

结果是这样的(我必须按 ; 才能生成所有结果)。

[[1, 2]]    
[[1, 2, 3]] 
[[2, 3]]

为什么 findall 的结果没有列表?

当计算 findall(SUBLIST, sublist(LIST, X, Y, SUBLIST), CASES) 时,X 和 Y 已经绑定到值。对于sublist([1, 2, 3], 1, 2, SUBLIST),只有一个解决方案([1, 2]),它在列表中找到所有returns。

回溯时,X 和 Y 会被赋予不同的值,并且会再次计算 findall,从而导致不同的单一结果。

您必须将成员调用移到 findall 中

test(LIST, CASES):-
   pairs(LIST, L), 
   findall(SUBLIST, (member(X/Y, L), sublist(LIST, X, Y,  SUBLIST)), CASES).

(我不再完全确定语法,但它是这样的)