function for greatest object in list less than or equal to a value

hh hh at
Mon Oct 12 18:24:26 EDT 2015

[@Bernd: here a PONG to your PING.]

Obviously each list member has to be inspected once for the comparison.
And each "smaller than"-member has to be inspected once again to find
the maximum. There is no other way, this is a math fact.

Here is my solution, that seems to be pretty fast.

-- v is a real num, L is a list of real comma-delimites nums
-- if L has NO member < v then return MINVAL (=your setting)
-- Empty members are treated as 0 (doesn't harm, if all x>0)
-- Pre-sorting of L gives no advantage.

function greatestMemberSmallerThan v,L
   put -10^15 into z[true] --> because max(empty)=0
   repeat for each item x in L
      -- if x is empty then next repeat --> here not necessary
      put comma & x after z[x<v]
   end repeat
   return max( z[true] )
end greatestMemberSmallerThan

More information about the use-livecode mailing list