Re: Standard library praise and challenge

new topic     » goto parent     » topic index » view thread      » older message » newer message

I agree that the library max function is no good.

I could understand someone writing max(flatten(S)) if that is what they wanted to do (why?). I don't understand why the argument to max should be flattened as part of the max function. It makes nonsense of the sort order of object.

I don't have a problem about returning the max of {} or the max of a number because max(3,3) is 3 and max({3}) is 3. I would rather max didn't return an error unless it had to (eg max()).

My function is:

  function max(sequence S) 
 
  -- accepts a (possibly empty) sequence of objects 
  -- returns either an 0 length list or a list containing the maximum item in the list 
 
    if length(S) < 2 then return S  
    else 
      sequence R = S[1] 
      for i = 2 to length(S) do 
        if S[i] > R[1] then R[1] = S[i] end if 
      end for 
      return R 
    end if 
  end function 
 
  max({1,2,3,4})  ==> {4} 
  max({ {},{} })  ==> { {} } 
  max({1,2,{0} }) ==> { {0} } 
  max({})         ==> { } 

All are exactly what you would expect, except (perhaps) for the last. And, returning the empty list as the max of an empty list is logical and should give no one headaches.

Bill

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu