%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Find the median M of a set S of integer numbers
% (the median is the unique element M of S such that there are as
% many elements of S which are smaller than M as there are elements
% of S which are larger tham M --- if S has an even number of
% elements, median/2 is assumed to be false
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:-consult_lib.
median(S,M) :-
(M in S &
xsize({Z : Z in S & Z < M},N1) &
xsize({Z : Z in S & Z > M},N2) &
N1 = N2)!.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Sample goal:
%
% {log}=> median({18,5,-3,7,1,7,11,2},M).
% M = 5