162
%avarietyofutilityfunctions.
%mergetwosortedlistsofaddresses.
merge(L1,[H|L2],[H|M]):-
less_than_head(H,L1),
!,
merge(L1,L2,M).
merge([H|L1],L2,[H|M]):-
merge(L1,L2,M).
merge([],L2,L2).
%Checkifanitemissmallerthantheheadofalist.Efficiency
%functionformerge.
less_than_head(X,[H|_]):-less(X,H).
%Mergesortforalistofaddresses.
mergesort([],[]).
mergesort([X],[X]).
mergesort(A,B):-
split(A,X,Y),
mergesort(X,XS),
mergesort(Y,YS),
merge(XS,YS,B),
!.
%Auxiliaryfunctionformergesortthatsplitsalistintoequal-sized
%pieces.
split([],[],[]).
split([A,B|T],[A|X],[B|Y]):-split(T,X,Y),!.
split([A],[A],[]).
%Listutilities:
append([],X,X).
append([X|Y],Z,[X|R]):-append(Y,Z,R).
ordered_list([]).
ordered_list([_]).
ordered_list([A,B|T]):-
less(A,B),
ordered_list([B|T]).
remove(_,[],[]).
163
remove(X,[X|M],R):-!,remove(X,M,R).
remove(X,[Y|L1],[Y|L2]):-remove(X,L1,L2).
reverse([],[]).
reverse([H|T],B):-
reverse(T,S),
append(S,[H],B).
member(X,[X|Y]).
member(X,[Y|L]):-member(X,L).
permutation(X,[H|T]):-
append(V,[H|U],X),
append(V,U,W),
permutation(W,T).
permutation([],[]).
permutation_dropping(S,Result):-
subset(S,SSub),
permutation(SSub,Result).
subset([],[]).
subset([Item|Items],[Item|Subset]):-
subset(Items,Subset).
subset([Item|Items],Subset):-
subset(Items,Subset).
Previous Page Next Page