如何在Prolog中使用另一个谓词中的列表?

时间:2020-03-23 16:29:30

标签: prolog predicate

我目前正在尝试解决此问题。

生成器generate/2将为窗口生成所有有效组合。

generate(Gs,Cs) :-
    colorset(Gs,Cs),
    valid(Gs,Cs). 

?- genere([b,c,d,i,j],C).

C = [red   , yellow, red   , yellow, blue  ] ;
C = [red   , yellow, red   , blue  , yellow] ;
C = [red   , yellow, blue  , yellow, red   ] ;
C = [red   , yellow, blue  , blue  , red   ] ;
C = [red   , yellow, blue  , blue  , yellow] ;
C = [red   , blue  , red   , yellow, blue  ] ;
C = [red   , blue  , red   , blue  , yellow] ;
C = [red   , blue  , yellow, yellow, red   ] ;
C = [red   , blue  , yellow, yellow, blue  ] ;
C = [red   , blue  , yellow, blue  , red   ] ;
C = [yellow, red   , yellow, red   , blue  ] ;
C = [yellow, red   , yellow, blue  , red   ] ;
C = [yellow, red   , blue  , red   , yellow] ;
C = [yellow, red   , blue  , blue  , red   ] ;
C = [yellow, red   , blue  , blue  , yellow] ;
C = [yellow, blue  , red   , red   , yellow] ;
C = [yellow, blue  , red   , red   , blue  ] ;
C = [yellow, blue  , red   , blue  , yellow] ;
C = [yellow, blue  , yellow, red   , blue  ] ;
C = [yellow, blue  , yellow, blue  , red   ] ;
C = [blue  , red   , yellow, red   , blue  ] ;
C = [blue  , red   , yellow, yellow, red   ] ;
C = [blue  , red   , yellow, yellow, blue  ] ;
C = [blue  , red   , blue  , red   , yellow] ;
C = [blue  , red   , blue  , yellow, red   ] ;
C = [blue  , yellow, red   , red   , yellow] ;
C = [blue  , yellow, red   , red   , blue  ] ;
C = [blue  , yellow, red   , yellow, blue  ] ;
C = [blue  , yellow, blue  , red   , yellow] ;
C = [blue  , yellow, blue  , yellow, red   ] ; 
false.

我需要谓词valid/2

给出我定义如下的谓词颜色集。

colorset([],[]).

colorset([_|Nodes], [C|Color]):- color(C), colorset(Nodes, Colors).

现在,我试图找出是否有一种方法可以首先生成颜色集谓词,然后检查相邻节点是否具有相同的颜色,但是我不确定在Prolog中是否可行?

另一种选择是什么?

0 个答案:

没有答案