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...
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))).