I’m trying to write a rule in prolog
adjacent(X,Y,Zs), as true if X and Y are adjacent to each other in the list Zs.
I currently have:
append([],L,L).
append([H|T],L,[H|LT]):-append(T,L,LT).
sublist(S,L):-append(_,S,P),append(P,_,L).
adjacent(X,Y,Zs):-sublist([X,Y],Zs).
test:
1 ?- sublist([1,2],[1,2,3,4]).
true .
2 ?- sublist([1,3],[1,2,3,4,5]).
ERROR: Out of global stack
3 ?-
Do you guys have any ideas? Thanks in advance.
that should work.
also, you have a predicate in the lists library called
nextto(?X, ?Y, ?List)that will do the same (but keep in mind that the semantics of this predicate is thatYfollowsXin the list, not just plain adjacent in any order).http://www.swi-prolog.org/pldoc/doc_for?object=section%282,%27A.12%27,swi%28%27/doc/Manual/lists.html%27%29%29