Greatest element of a list: Difference between revisions

Content deleted Content added
Added solution for Action!
Not a robot (talk | contribs)
Add CLU
Line 1,095: Line 1,095:
;; If the values are already in a collection, use apply:
;; If the values are already in a collection, use apply:
(apply max [1 2 3 4]) ; evaluates to 4</lang>
(apply max [1 2 3 4]) ; evaluates to 4</lang>

=={{header|CLU}}==
<lang clu>% 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</lang>

{{out}}
<pre>maximum int: 99
maximum real: 3.140000e+00</pre>


=={{header|CMake}}==
=={{header|CMake}}==