Trying to write Prolog rules that incorporates the following rules for a function matchFirstTwo(List1, List2)
, that evaluates to true if the last two elements of the first list = the first two elements of the second list. Append and reverse cannot be implemented into the solution.
last([X], X).
last([_|Z], X) :- last(Z, X).
nextToLast([X,_], X).
nextToLast([_|Z], X) :- nextToLast(Z, X).
Write matchFirstTwo(List1, List2)
— succeeds exactly when List2
's first and second elements equal List1
's last two elements
% Example :
?- matchFirstTwo([a,b,c,d],[d,c,a,b]).
true.
matchFirstTwo([a],[a])
? – falseList2's first and second element are equals
btw use recursion and when you reach length of 2 then simply compare them – Luai Ghunim