Let's first think about what “neighbors” mean in the list: in what cases Aand Bneighboring elements in the list?
The answer . If the list has a form [...,X,Y,...]and at least one of the following actions is performed:
A = X and B = YA = Yand B = X.
: (A = X & and; B = Y) & or; (A = Y & and; B = X).
, :
&; ((A = X & and; B = Y) & or; (A = Y & and; B = X)) & equiv;
& ; &; (A = X & and; B = Y) &; &; (A = Y & and; B = X) & equiv;
& ; (& not; A = X & or; & not; B = Y) & &; (& not; A = Y & or; & not; B = X) & equiv;
& ; (A & ne; X & or; B & ne; Y) & &; (A & ne; Y & or; B & ne; X)
, - , ?
X & ne; Y Prolog, dif/2, @CapelliC. dif/2 , . pure . Prolog , , , ! iso_dif/2, .
Prolog :
( dif(A, X) ; dif(B, Y) ), ( dif(A, Y) ; dif(B, X) )
(',')/2 , (;)/2 .
, :
not_neighbours(_, _, []).
not_neighbours(_, _, [_]).
not_neighbours(A, B, [X,Y|Rest]) :-
( dif(A, X) ; dif(B, Y) ),
( dif(A, Y) ; dif(B, X) ),
not_neighbours(A, B, [Y|Rest]).
:
?- not_neighbours(a, b, [a,b]).
false.
?- not_neighbours(A, B, [A,B]).
false.
?- not_neighbours(A, B, [_,A,B|_]).
false.
?- not_neighbours(A, B, [_,B,A|_]).
false.
?- not_neighbours(a, b, [_,a,c,_]).
true .
, , , .
, (;)/2 , . , :
& ; A & ; B & equiv; A & rightarrow;
, (& not; A = X & or; & not; B = Y) A = X & rightarrow; B & ne; Y.
implication Prolog if_/3:
impl(A, B) :- if_(A, B, true).
:
not_neighbours(_, _, []).
not_neighbours(_, _, [_]).
not_neighbours(A, B, [X,Y|Rest]) :-
impl(A=X, dif(B, Y)),
impl(B=X, dif(A, Y)),
not_neighbours(A, B, [Y|Rest]).
:
?- not_neighbours(a, b, [x,y]).
true ;
false.
:
?- not_neighbours(a, b, [X,Y]).
X = a,
dif(Y, b) ;
X = b,
dif(Y, a) ;
dif(X, b),
dif(X, a) ;
false.
. , , , :
?- length(Ls, _), not_neighbours(A, B, Ls).
, Prolog.
dif/2 , Prolog . dif/2 () , , . , , dif/2.