0

这里的任务是在 prolog 中编写一个颜色图。这是我的代码

/* Neighboring relationship*/
edge(wa, or). /* washington is a neighbor of oregon */
edge(wa, id). /* washington is a neighbor of idaho */
edge(or, ca). /* oregon is a neighbor of california */
edge(or, ne). /* oregon is a neighbor of nevada */
edge(or, id). /* oregon is a neighbor of idaho */
edge(id, mo). /* idaho is a neighbor of montana */
edge(id, wy). /* idaho is a neighbor of wyoming */
edge(id, ne). /* idaho is a neighbor of nevada */
edge(id, ut). /* idaho is a neighbor of utah */
edge(mo, wy). /* montana is a neighbor of wyoming */
edge(wy, ut). /* wyoming is a neighbor of utah */
edge(wy, co). /* wyoming is a neighbor of colorado */
edge(ca, ne). /* california is a neighbor of nevada */
edge(ca, ar). /* california is a neighbor of arizona */
edge(ne, ut). /* nevada is a neighbor of utah */
edge(ne, ar). /* nevada is a neighbor of arizona */
edge(ut, nm). /* utah is a neighbor of new mexico */
edge(ut, ar). /* utah is a neighbor of arizona */
edge(ut, co). /* utah is a neighbor of colorado */

/* Colors of the states*/
color(wy, yellow). /* wyoming is painted yellow */
color(wa, yellow). /* washington is painted yellow */
color(ne, yellow). /* nevada is painted yellow */
color(mo, green). /* montana is painted green */
color(ca, green). /* california is painted green */
color(nm, green). /* new mexico is painted green */
color(ut, red).   /* utah is painted red */
color(or, red).   /* oregon is painted red */
color(ha, red).   /* hawaii is painted red */
color(al, red).   /* alaska is painted red */
color(co, orange). /* colorado is painted orange */
color(ar, orange). /* arizona is painted orange */
color(id, orange). /* idaho is painted orange */


/*  adjacent rule*/
adjacent(X, Y) :- edge(X, Y); edge(Y, X).
connected(Node1, Node2) :- edge(Node1, Node2).
connected(Node1, Node2) :- edge(Node1, X), connected(X, Node2).

/*  miscolor rule*/
miscolor(S1, S2, Color) :-
    adjacent(S1, S2), color(S1, Color), color(S2, Color).

/* condition rule to test the miscolor rule */  
q :- miscolor(S1, S2, Color).

这是最困扰我的:写一个名为 q :- 条件的规则。测试时,输入 q 将测试您的错误颜色规则。

该代码确实有效,但编译器给出了错误消息:question1.pl:62: warning: singleton variables [S1,S2,Color] for q/0。请帮助我解决导致错误的最后一个条件。

4

1 回答 1

1

这个警告是完全正确的。来自 swi-prolog 手册

A singleton variable is a variable that appears only one time in a clause

https://apps.nms.kcl.ac.uk/reactome-pengine/documentation/man?section=singleton

所以你应该更正如下定义

q :- miscolor(_, _, _).

因为您定义了一些不在任何地方使用的变量。

如果你想显示所有的错色对,你应该使用 sth like

miscolor(A,B,C).
于 2021-10-03T21:27:32.487 回答