162
% a variety of utility functions.
% merge two sorted lists of addresses.
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).
% Check if an item is smaller than the head of a list. Efficiency
% function for merge.
less_than_head(X, [H | _]) :- less(X, H).
% Mergesort for a list of addresses.
mergesort([], []).
mergesort([X], [X]).
mergesort(A, B) :-
split(A, X, Y),
mergesort(X, XS),
mergesort(Y, YS),
merge(XS, YS, B),
!.
% Auxiliary function for mergesort that splits a list into equal-sized
% pieces.
split([], [], []).
split([A, B |T], [A|X], [B|Y]) :- split(T, X, Y), !.
split([A], [A], []).
% List utilities:
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(_, [], []).
% a variety of utility functions.
% merge two sorted lists of addresses.
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).
% Check if an item is smaller than the head of a list. Efficiency
% function for merge.
less_than_head(X, [H | _]) :- less(X, H).
% Mergesort for a list of addresses.
mergesort([], []).
mergesort([X], [X]).
mergesort(A, B) :-
split(A, X, Y),
mergesort(X, XS),
mergesort(Y, YS),
merge(XS, YS, B),
!.
% Auxiliary function for mergesort that splits a list into equal-sized
% pieces.
split([], [], []).
split([A, B |T], [A|X], [B|Y]) :- split(T, X, Y), !.
split([A], [A], []).
% List utilities:
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(_, [], []).