% append(Xs,Ys,Zs) <=>
% Xs = [A1,...,Ak], Ys = [B1,...,Bm], Zs = [A1,...,Ak,B1,...Bm]

% pattern-oriented definition:
append([],    Ys,Ys).
append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs).

%  ?- append([a,b],[c],[a,b,c]).
%    |- clause 2
%  ?- append([b],[c],[b,c]).
%    |- clause 2
%  ?- append([],[c],[c]).
%    |- clause 1
%  ?- .

% add some element at the end of a list:
add_list(L,E,LwithE) :- append(L,[E],LwithE).

% last element of a list:
last(L,E) :- append(_,[E],L).

% element of a list:
elem(E,L) :- append(_,[E|_],L).

% delete some occurrence of an element in a list:
delete(L1,E,L2) :-
        append(Xs,[E|Ys],L1),
        append(Xs,Ys,L2).

% is a list SL a sublist of a list Xs:
sublist(SL,Xs) :-
        append(_,TL2,Xs),
        append(SL,_,TL2).
