如果没有一些样本数据和预期/期望的结果,很难说出您需要什么。但是...检查谓词中参数的状态可能会为您提供所需的内容。
例如,给定以下数据:
foo( albania , 1 ) .
foo( albania , 2 ) .
foo( albania , 3 ) .
foo( albania , 4 ) .
foo( albania , 5 ) .
foo( albania , 6 ) .
foo( iceland , 4 ) .
foo( iceland , 5 ) .
foo( iceland , 6 ) .
foo( iceland , 7 ) .
foo( iceland , 8 ) .
foo( iceland , 9 ) .
foo( france , 7 ) .
foo( france , 8 ) .
foo( france , 9 ) .
foo( france , 10 ) .
foo( france , 11 ) .
foo( france , 12 ) .
您对问题中显示的内容的初步看法
predicate(Country, X):-
setof(Length, foo(Country,Length), X).
Country
以未绑定变量调用时返回以下多个结果:
?- predicate(Country,X).
Country = albania , X = [1, 2, 3, 4, 5, 6] ;
Country = france , X = [7, 8, 9, 10, 11, 12] ;
Country = iceland , X = [4, 5, 6, 7, 8, 9] .
Country
如果在绑定到有效国家/地区的情况下调用这个单一结果,iceland
在这种情况下:
?- predicate(iceland,X).
X = [4, 5, 6, 7, 8, 9].
然而,如果你做这样的事情,
predicate( C , Xs ) :- var(C) , setof( X , C^foo(C,X) , Xs ) .
predicate( C , Xs ) :- nonvar(C) , setof( X , foo(C,X) , Xs ) .
当参数未绑定时,您将获得这个解决方案:
?- try_this(C,Xs).
Xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] ;
false.
你会注意到它C
仍然不受约束。
当参数被绑定时,你会得到这个结果:
?- try_this(iceland,Xs).
Xs = [4, 5, 6, 7, 8, 9].