Q:

How can i convert a list into a circular list in Prolog?

I have a list [5, 4, 8, 9, 7, 6], I need compare each digit with next to digit, also compare first digit and last digit to each other. Think like that this list is around a circular table. Any help will be appreciated. Thanks now...

A:

It is likely you do not need a circular list—a non-circular one will do...

:- use_module(library(lists), [last/2]).

How so? Quite straightforwardly: prepend the last item and use "lagging" to enforce your constraint of choice between all adjacent list items.

wrapped_adj_dif([E|Es]) :-
   last(Es, E0),
   adj_dif_prev([E|Es], E0).

adj_dif_prev([], _).
adj_dif_prev([E|Es], E0) :-
   dif(E, E0),
   adj_dif_prev(Es, E).

Sample queries1 using SICStus Prolog 4.3.2:
?- wrapped_adj_dif(Xs).
   Xs = [_A,_B]      , dif(_B,_A), dif(_A,_B)
;  Xs = [_A,_B,_C]   , dif(_C,_A), dif(_A,_B), dif(_B,_C)
;  Xs = [_A,_B,_C,_D], dif(_D,_A), dif(_A,_B), dif(_B,_C), dif(_C,_D)
...

Notice that above code is deterministic when the first argument is an acyclic ground list:

?- wrapped_adj_dif([a,b,c,d]).
true.
?- wrapped_adj_dif([a,b,c,a]).
false.

Edit
If you use the above "wrapped adjacent" access pattern with different constraints, consider hoisting the common features! You can cast them into a reusable meta-predicate like so:

for_all_wrapped_adjacent(P_2, Es) :-
   last(Es, E0),
   i_mapadj_prev(Es, P_2, E0).

i_mapadj_prev([], _, _).
i_mapadj_prev([E|Es], P_2, E0) :-
   call(P_2, E0, E),
   i_mapadj_prev(Es, P_2, E).

Using above meta-predicate
the new definition of wrapped_adj_difNU/1
boils down to:

wrapped_adj_difNU(Es) :-
   for_all_wrapped_adjacent(dif, Es).

Here are above queries again2—this time using wrapped_adj_difNU/1:
?- wrapped_adj_difNU([a,b,c,d]).
true.                                                   % (unchanged)

?- wrapped_adj_difNU([a,b,c,a]).
false.                                                  % (unchanged)

?- wrapped_adj_difNU(Xs).
   Xs = [_A,_B]      , dif(_B,_A), dif(_A,_B)
;  Xs = [_A,_B,_C]   , dif(_C,_A), dif(_A,_B), dif(_B,_C)
;  Xs = [_A,_B,_C,_D], dif(_D,_A), dif(_A,_B), dif(_B,_C), dif(_C,_D)
...                                                     % (unchanged)

Footnote 1: 
The prolog-toplevel answers were post-processed manually for the sake of conciseness and readability.

Footnote 2: No need to worry! The answers Prolog gives us have not changed—not at all.

A:

just carry on the first element during a recursive visit, stopping at last:
compare_adj([First|Rest]) :- compare_adj([First|Rest], First).
compare_adj([A,B|T], F) :-
 writeln(compare(A,B)),
 compare_adj([B|T], F).
compare_adj([Last], First) :-
 writeln(compare(Last, First)).

?- compare_adj([5, 4, 8, 9, 7, 6]).
compare(5,4)
compare(4,8)
compare(8,9)
compare(9,7)
compare(7,6)
compare(6,5)
true ;
false.

edit
albeit fairly inefficient, we could do combining some builtins, to spare some code:
compare_adj([First|Rest]) :-
 append([First|Rest],[First],Temp),
 forall(append(_, [A,B|_], Temp), writeln(compare(A,B))).

