?- append([a,b],[c],R). R=[a,b,c] ?- append(_,[e,s],[f,o,x,e,s]). yes ?- append(X,[e,s],[f,o,x,e,s]). X=[f,o,x] ?-append(X,Y,[a,b,c]). X=[] Y=[a,b,c] X=[a] Y=[b,c] X=[a,b] Y=[c] X=[a,b,c] Y=[] no
append([],L,L). append([H|T],L,[H|R]):- append(T,L,R).
Example. last3/2 finds the last 3 elements of the list. ?-last3([c,o,l,l,e,g,e],R). R=[e,g,e]
last3(L,[A1,A2,A3]):- append(_,[A1,A2,A3],L). or last3(L,[A1,A2,A3]):- append(_,X,L), length(X,3).