0

这很简单,但似乎无法掌握我有这些“颜色”

color(blue).
color(red).
color(white).

使用setof我需要在列表中获取这些颜色的所有可能组合如果您能提供一个简短的解释,那就太好了。我试过这个查询

setof(X,color(X),Colors).显然失败了

谢谢

4

2 回答 2

2

我想你的意思是组合:

?- setof((X,Y), (color(X), color(Y)), ColorsCombined).
ColorsCombined = [ (blue, blue), (blue, green), (blue, red), (green, blue), (green, green), (green, red), (red, blue), (red, green), (..., ...)].

或者你的意思是超集?

subset([Element|Set], [Element|Subset]):- subset(Set, Subset).
subset([_|Set], Subset):- subset(Set, Subset).
subset([], []).

superset(Set, Superset) :- setof(Subset, subset(Set, Subset), Superset).

这是输出:

 ?- superset([1,2,3], Superset).
Superset = [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]].
于 2014-02-13T20:53:06.243 回答
2

你的意思是这样吗?

all_permutations(Permutations) :-

    % get all colors into a list
    setof(Color, color(Color), One_Color_List),

    % find all combinations using permutation/2 on One_Color_List
    setof(Permutation, 
       permutation(One_Color_List, Permutation), 
       Permutations).

结果:

?- all_permutations(X).
X = [[blue, red, white], [blue, white, red], [red, blue, white], [red, white, blue], [white, blue, red], [white, red, blue]].

诀窍是将事实放入一个列表中 - 正如您所做的那样,然后使用 permutation/2 生成该列表的所有排列。

如果这就是你想要的......不清楚......

于 2014-02-13T21:43:55.397 回答