append([ ], List, List).What if the first list isn't empty? We need to take its head, save it, and stick it onto the front of the result. The result itself comes about as the consequence of carrying out the same procedures on the tail of the list. This is the recursive clause:
append([Head|Tail], List, [Head|Result]):- append(Tail, List, Result).append/3 can also be used to generate solutions. Here are some examples:
|?- append([a,b], [c,d], X). X = [a,b,c,d] ? yes
|?- append(X, [c,d], [a,b,c,d]). X = [a,b] ? yes
|?- append([a,b], X, [a,b,c,d]). X = [c,d] ? yes
|?- append(X, Y, [a,b,c,d]). X = [ ], Y = [a,b,c,d] ? ; X = [a], Y = [b,c,d] ? ; X = [a,b], Y = [c,d] ? ; X = [a,b,c], Y = [d] ? ; X = [a,b,c,d], Y = [ ] ? ;