Функция бинарного поиска

function FoundByBinarySearch
(
LowIdx,
HighIdx : LongInt;
var   Result  : LongInt;
const GoalIs  : CompareFunc;
var   Data;
var   Goal
) : Boolean;
var
CompVal : CompareResults;
begin
FoundByBinarySearch := FALSE;

if HighIdx < LowIdx then
Exit;

Result := LowIdx + ((HighIdx-LowIdx) div 2);
CompVal := GoalIs(Result, Data, Goal);

if CompVal = BinEqual then
FoundByBinarySearch := TRUE
else if (LowIdx < HighIdx) then
begin
if CompVal = BinLess then
HighIdx := Result-1
else {CompVal = BinGreater}
LowIdx  := Result+1;
FoundByBinarySearch := FoundByBinarySearch(
LowIdx, HighIdx, Result, GoalIs, Data, Goal)
end
else if (CompVal = BinLess) then
Dec(Result)
end; { function FoundByBinarySearch }