爲了建立關係變量之間:
mask_helper(_, _, [], []).
mask_helper(A, B, [A|_], [B|_]):- !.
mask_helper(A, B, [X|Xs], [_|Ys]):-
A \= X,
mask_helper(A, B, Xs, Ys).
mask([], []).
mask([X|Xs], [Y|Ys]):-
mask_helper(X,Y,Xs,Ys),
mask(Xs, Ys).
,你可以建立你的面具:
?- mask([red,red,blue],X).
X = [_G300, _G300, _G306] .
但:
?- mask([red,red,blue],X), mask([blue,red,red],Y), X=Y.
X = [_G27, _G27, _G27],
Y = [_G27, _G27, _G27].
即如果您將使用assert
(沒有規則正文),則與Color1 \= Color2
沒有任何關係。
你可以考慮像事端顏色分配(很流行的做法)的順序:
colour(red). colour(green). colour(blue).
colour_order(red, red).
colour_order(red, green).
colour_order(red, blue).
colour_order(green, green).
colour_order(green, blue).
colour_code([]).
colour_code([X]):- colour(X).
colour_code([X|[Y|T]]):-
colour_order(X,Y),
colour_code([Y|T]).
map_color(A,B,C):-
colour_code([A,B,C]),
C \= B, C \= A.
但同樣,你永遠不會導致「紅,藍,紅」,如果你的條件將是A \= B, B \= C
。
想想:
unify([], [], _).
unify([X|Xs], [Y|Ys], M):-
member((X=Z), M), !,
Z = Y,
unify(Xs, Ys, M).
unify([X|Xs], [Y|Ys], M):-
% X is not assigned yet
not(member((_=Y),M)), % Y is not used as well
unify(Xs, Ys, [(X=Y)|M]).
比你可以比較:
?- unify([red,red,blue],[blue,blue,red],[]).
true.
?- unify([red,red,blue],[blue,blue,blue],[]).
false.
?- unify([red,red,blue],[blue,red,red],[]).
false.