%%% CSE 428: Examples of Prolog programs on lists

%%% Append of two lists:
%%% append(L,K,M) holds iff M is the result of appending L and K

append([],K,K).
append([X|L],K,[X|M]) :- append(L,K,M).

%%% Permutations of a list:
%%% perm(L,K) holds iff K is a permutation of L

perm([],[]).
perm(L,[X|K]) :- append(L1,[X|L2],L), append(L1,L2,R), perm(R,K).

%%% Removing an element from a list
%%% delete(X,L,K) holds iff K is the result of deleting 
%%% all occurrences of X from L

delete(_,[],[]).
delete(X,[X|L],K) :- delete(X,L,K).
delete(X,[Y|L],[Y|K]) :- not(X=Y), delete(X,L,K).
