简单Prolog集合
这很简单,但似乎无法理解 我有这些“颜色” 使用简单Prolog集合,prolog,logic,prolog-setof,Prolog,Logic,Prolog Setof,这很简单,但似乎无法理解 我有这些“颜色” 使用setof我需要在列表中获得这些颜色的所有可能组合 如果你能提供一个简短的解释,那就太好了。我试过这个问题 setof(X,color(X),Colors)。显然失败了 谢谢我想你的意思是组合: ?- setof((X,Y), (color(X), color(Y)), ColorsCombined). ColorsCombined = [ (blue, blue), (blue, green), (blue, red), (green, blue
setof
我需要在列表中获得这些颜色的所有可能组合
如果你能提供一个简短的解释,那就太好了。我试过这个问题
setof(X,color(X),Colors)。
显然失败了
谢谢我想你的意思是组合:
?- 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]].
我想你的意思是组合:
?- 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]].
你是说像这样
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生成该列表的所有置换
如果那是你想要的。。不清楚…你是说这样
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生成该列表的所有置换
如果那是你想要的。。不清楚…谢谢,这基本上就是我想要的!!为了进一步理解,我使用了这个查询集((X,Y,Z),(color(X),color(Y),color(Z)),ColorsCombined)。有三个项目的元组,但我想有一个列表。e、 g ColorsCombined=[(红色,红色),…]我想要的是颜色Combined[[红色,红色],…]我需要更改什么?将括号从()更改为[]谢谢,这基本上就是我想要的!!为了进一步理解,我使用了这个查询集((X,Y,Z),(color(X),color(Y),color(Z)),ColorsCombined)。有三个项目的元组,但我想有一个列表。e、 g ColorsCombined=[(红色,红色),…]我想要的是颜色Combined[[红色,红色],…]我需要更改什么?将括号从()更改为[]实际上是我想要的,谢谢!!事实上是的,这就是我想要的,谢谢!!