Task

Create a function that returns the maximum value in a provided set of values,

where the number of values may not be known until run-time.




% This "maximum" procedure is fully general, as long as
% the container type has an elements iterator and the 
% data type is comparable.
% It raises an exception ("empty") if there are no elements.
 
maximum = proc [T,U: type] (a: T) returns (U) 
          signals (empty)
          where T has elements: itertype (T) yields (U),
                U has gt: proctype (U,U) returns (bool)
    max: U
    seen: bool := false
 
    for item: U in T$elements(a) do
        if ~seen cor item > max then 
            max := item 
            seen := true
        end
    end
    if (~seen) then
        signal empty
    else
        return(max)
    end
end maximum
 
 
start_up = proc ()
    po: stream := stream$primary_output()
 
    % try it on an array of ints
    ints: array[int] := array[int]$[1,5,17,2,53,99,61,3]
    imax: int := maximum[array[int], int](ints)
    stream$putl(po, "maximum int: " || int$unparse(imax))    
 
    % try it on a sequence of reals
    reals: sequence[real] := sequence[real]$[-0.5, 2.6, 3.14, 2.72]
    rmax: real := maximum[sequence[real], real](reals)
    stream$putl(po, "maximum real: " || real$unparse(rmax))
end start_up