Greatest element of a list: Difference between revisions
Content deleted Content added
→{{header|Python}}: Added a version covering non-numeric values. |
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
||
Line 9: | Line 9: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
11l already has a "Maximum Value" function. |
11l already has a "Maximum Value" function. |
||
<lang |
<syntaxhighlight lang="11l">max(values)</syntaxhighlight> |
||
=={{header|8th}}== |
=={{header|8th}}== |
||
< |
<syntaxhighlight lang="forth"> |
||
[ 1.0, 2.3, 1.1, 5.0, 3, 2.8, 2.01, 3.14159 ] ' n:max 0 a:reduce . cr |
[ 1.0, 2.3, 1.1, 5.0, 3, 2.8, 2.01, 3.14159 ] ' n:max 0 a:reduce . cr |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: 5 |
Output: 5 |
||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang="aarch64 assembly"> |
|||
<lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program rechMax64.s */ |
/* program rechMax64.s */ |
||
Line 140: | Line 140: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 147: | Line 147: | ||
=={{header|ACL2}}== |
=={{header|ACL2}}== |
||
< |
<syntaxhighlight lang="lisp">(defun maximum (xs) |
||
(if (endp (rest xs)) |
(if (endp (rest xs)) |
||
(first xs) |
(first xs) |
||
(max (first xs) |
(max (first xs) |
||
(maximum (rest xs)))))</ |
(maximum (rest xs)))))</syntaxhighlight> |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">BYTE FUNC Max(BYTE ARRAY tab BYTE size) |
||
BYTE i,res |
BYTE i,res |
||
Line 184: | Line 184: | ||
m=Max(tab,size) |
m=Max(tab,size) |
||
PrintF("Greatest: %I%E",m) |
PrintF("Greatest: %I%E",m) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Greatest_element_of_a_list.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Greatest_element_of_a_list.png Screenshot from Atari 8-bit computer] |
||
Line 193: | Line 193: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
< |
<syntaxhighlight lang="actionscript">function max(... args):Number |
||
{ |
{ |
||
var curMax:Number = -Infinity; |
var curMax:Number = -Infinity; |
||
Line 199: | Line 199: | ||
curMax = Math.max(curMax, args[i]); |
curMax = Math.max(curMax, args[i]); |
||
return curMax; |
return curMax; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
The keys for this task are initializing the compared value to the 'First value of the element type, and use of an unconstrained array type. |
The keys for this task are initializing the compared value to the 'First value of the element type, and use of an unconstrained array type. |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_Io; |
||
procedure Max_Test isco |
procedure Max_Test isco |
||
Line 230: | Line 230: | ||
begin |
begin |
||
Ada.Text_IO.Put_Line(Float'Image(Max(Buf))); |
Ada.Text_IO.Put_Line(Float'Image(Max(Buf))); |
||
end Max_Test;</ |
end Max_Test;</syntaxhighlight> |
||
A generic function Max to deal with any floating-point type. |
A generic function Max to deal with any floating-point type. |
||
< |
<syntaxhighlight lang="ada">generic |
||
type Item is digits <>; |
type Item is digits <>; |
||
type Items_Array is array (Positive range <>) of Item; |
type Items_Array is array (Positive range <>) of Item; |
||
function Generic_Max (List : Items_Array) return Item;</ |
function Generic_Max (List : Items_Array) return Item;</syntaxhighlight> |
||
Implementation of: |
Implementation of: |
||
< |
<syntaxhighlight lang="ada">function Generic_Max (List : Items_Array) return Item is |
||
Result : Item := List (List'First); |
Result : Item := List (List'First); |
||
begin |
begin |
||
Line 244: | Line 244: | ||
end loop; |
end loop; |
||
return Result; |
return Result; |
||
end Generic_Max;</ |
end Generic_Max;</syntaxhighlight> |
||
When the argument array is empty, Constraint_Error exception is propagated, because array indexing is checked in Ada. Note also use of the floating-type attribute Max. |
When the argument array is empty, Constraint_Error exception is propagated, because array indexing is checked in Ada. Note also use of the floating-type attribute Max. |
||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">integer |
||
lmax(list l) |
lmax(list l) |
||
{ |
{ |
||
Line 262: | Line 262: | ||
max; |
max; |
||
}</ |
}</syntaxhighlight> |
||
or |
or |
||
< |
<syntaxhighlight lang="aime">integer |
||
lmax(list l) |
lmax(list l) |
||
{ |
{ |
||
Line 273: | Line 273: | ||
max; |
max; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 280: | Line 280: | ||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}} |
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}} |
||
< |
<syntaxhighlight lang="algol68"># substitute any array type with a scalar element # |
||
MODE FLT = REAL; |
MODE FLT = REAL; |
||
Line 309: | Line 309: | ||
except empty array: |
except empty array: |
||
SKIP |
SKIP |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 316: | Line 316: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
< |
<syntaxhighlight lang="algolw">begin |
||
% simple list type % |
% simple list type % |
||
record IntList( integer val; reference(IntList) next ); |
record IntList( integer val; reference(IntList) next ); |
||
Line 342: | Line 342: | ||
write( maxElement( IntList( -767, IntList( 2397, IntList( 204, null ) ) ) ) ) |
write( maxElement( IntList( -767, IntList( 2397, IntList( 204, null ) ) ) ) ) |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 350: | Line 350: | ||
=={{header|Amazing Hopper}}== |
=={{header|Amazing Hopper}}== |
||
Version 1: |
Version 1: |
||
<syntaxhighlight lang="amazing hopper"> |
|||
<lang Amazing Hopper> |
|||
#include <hopper.h> |
#include <hopper.h> |
||
main: |
main: |
||
Line 369: | Line 369: | ||
{"\n"} print |
{"\n"} print |
||
exit(0) |
exit(0) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Version 2: |
Version 2: |
||
<syntaxhighlight lang="amazing hopper"> |
|||
<lang Amazing Hopper> |
|||
#include <hopper.h> |
#include <hopper.h> |
||
#define SIZE_LIST 100000 |
#define SIZE_LIST 100000 |
||
Line 387: | Line 387: | ||
{"\n"} print |
{"\n"} print |
||
exit(0) |
exit(0) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 394: | Line 394: | ||
=={{header|AntLang}}== |
=={{header|AntLang}}== |
||
<lang |
<syntaxhighlight lang="antlang">max|range[10]</syntaxhighlight> |
||
=={{header|APL}}== |
=={{header|APL}}== |
||
< |
<syntaxhighlight lang="apl">LIST←2 4 6 3 8 |
||
⌈/LIST</ |
⌈/LIST</syntaxhighlight> |
||
{{out}} <pre>8</pre> |
{{out}} <pre>8</pre> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
<syntaxhighlight lang="applescript"> |
|||
<lang AppleScript> |
|||
max({1, 2, 3, 4, 20, 6, 11, 3, 9, 7}) |
max({1, 2, 3, 4, 20, 6, 11, 3, 9, 7}) |
||
Line 412: | Line 412: | ||
return _curMax |
return _curMax |
||
end max |
end max |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 419: | Line 419: | ||
{{trans|JavaScript}} |
{{trans|JavaScript}} |
||
<syntaxhighlight lang="applescript"> |
|||
<lang AppleScript> |
|||
-- maximumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a |
-- maximumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a |
||
on maximumByMay(f, xs) |
on maximumByMay(f, xs) |
||
Line 569: | Line 569: | ||
end script |
end script |
||
end if |
end if |
||
end mReturn</ |
end mReturn</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
< |
<syntaxhighlight lang="applescript">{"epsilon", {name:"Shanghai", population:24.15}}</syntaxhighlight> |
||
=={{header|Applesoft BASIC}}== |
=={{header|Applesoft BASIC}}== |
||
< |
<syntaxhighlight lang="applesoftbasic"> 100 REMMAX |
||
110 R$ = "":E$ = "" |
110 R$ = "":E$ = "" |
||
120 L = LEN (L$) |
120 L = LEN (L$) |
||
Line 593: | Line 593: | ||
270 R = VAL (V$) |
270 R = VAL (V$) |
||
280 IF R < V THEN RETURN |
280 IF R < V THEN RETURN |
||
290 R$ = V$: RETURN</ |
290 R$ = V$: RETURN</syntaxhighlight> |
||
< |
<syntaxhighlight lang="applesoftbasic">L$ = "1 2 3 4 20 6 11 3 9 7" |
||
GOSUB 100MAX |
GOSUB 100MAX |
||
PRINT R$</ |
PRINT R$</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>20</pre> |
<pre>20</pre> |
||
Line 602: | Line 602: | ||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
Line 815: | Line 815: | ||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">arr: [5 4 2 9 7 3] |
||
print max arr</ |
print max arr</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 828: | Line 828: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
=== CSV Data === |
=== CSV Data === |
||
< |
<syntaxhighlight lang="autohotkey">list = 1,5,17,-2 |
||
Loop Parse, list, `, |
Loop Parse, list, `, |
||
x := x < A_LoopField ? A_LoopField : x |
x := x < A_LoopField ? A_LoopField : x |
||
MsgBox Max = %x%</ |
MsgBox Max = %x%</syntaxhighlight> |
||
=== Pseudo-arrays === |
=== Pseudo-arrays === |
||
< |
<syntaxhighlight lang="ahk">list = 1,5,17,-2 |
||
StringSplit, list, list,`, ; creates a pseudo-array |
StringSplit, list, list,`, ; creates a pseudo-array |
||
Loop % List0 |
Loop % List0 |
||
x := x < List%A_Index% ? List%A_Index% : x |
x := x < List%A_Index% ? List%A_Index% : x |
||
MsgBox Max = %x%</ |
MsgBox Max = %x%</syntaxhighlight> |
||
=== True arrays === |
=== True arrays === |
||
{{works with|AutoHotkey_L}} |
{{works with|AutoHotkey_L}} |
||
< |
<syntaxhighlight lang="ahk">List := [1,5,17,-2] |
||
For each, value in List |
For each, value in List |
||
x := x < value ? value : x |
x := x < value ? value : x |
||
MsgBox Max = %x%</ |
MsgBox Max = %x%</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
One-liner: |
One-liner: |
||
< |
<syntaxhighlight lang="awk">$ awk 'func max(a){for(i in a)if(a[i]>r)r=a[i];return r}BEGIN{a[0]=42;a[1]=33;a[2]=21;print max(a)}' |
||
42</ |
42</syntaxhighlight> |
||
More readable version: |
More readable version: |
||
< |
<syntaxhighlight lang="awk"> |
||
# Usage: awk -f greatest_list_element.awk |
# Usage: awk -f greatest_list_element.awk |
||
# |
# |
||
Line 864: | Line 864: | ||
print max(a) |
print max(a) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Axe}}== |
=={{header|Axe}}== |
||
This example assumes the array is null-terminated so that the program can stop at the end of the data. |
This example assumes the array is null-terminated so that the program can stop at the end of the data. |
||
< |
<syntaxhighlight lang="axe">Lbl MAX |
||
0→M |
0→M |
||
While {r₁} |
While {r₁} |
||
Line 874: | Line 874: | ||
End |
End |
||
M |
M |
||
Return</ |
Return</syntaxhighlight> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
{{works with|QBasic}} |
{{works with|QBasic}} |
||
< |
<syntaxhighlight lang="qbasic">DECLARE SUB addVal (value AS INTEGER) |
||
DECLARE FUNCTION findMax% () |
DECLARE FUNCTION findMax% () |
||
Line 924: | Line 924: | ||
NEXT |
NEXT |
||
findMax = tmp2 |
findMax = tmp2 |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 932: | Line 932: | ||
==={{header|BaCon}}=== |
==={{header|BaCon}}=== |
||
< |
<syntaxhighlight lang="freebasic">' Greatest element from list |
||
' Populate sample array of numbers |
' Populate sample array of numbers |
||
READ elements |
READ elements |
||
Line 953: | Line 953: | ||
NEXT |
NEXT |
||
RETURN mx |
RETURN mx |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 963: | Line 963: | ||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
{{trans|Yabasic}} |
{{trans|Yabasic}} |
||
< |
<syntaxhighlight lang="freebasic">l$ = "1,1234,62,234,12,34,6" |
||
dim n$(1) |
dim n$(1) |
||
n$ = explode(l$, ",") |
n$ = explode(l$, ",") |
||
Line 974: | Line 974: | ||
next i |
next i |
||
print "Alphabetic order: "; m$; ", numeric order: "; m</ |
print "Alphabetic order: "; m$; ", numeric order: "; m</syntaxhighlight> |
||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang="dos">::max.cmd |
||
@echo off |
@echo off |
||
setlocal enabledelayedexpansion |
setlocal enabledelayedexpansion |
||
Line 993: | Line 993: | ||
if "%2" equ "" goto :eof |
if "%2" equ "" goto :eof |
||
if %2 gtr !%1! set res=%2 |
if %2 gtr !%1! set res=%2 |
||
goto loop</ |
goto loop</syntaxhighlight> |
||
''Invocation from command line or from internal prompt'' |
''Invocation from command line or from internal prompt'' |
||
< |
<syntaxhighlight lang="dos">>max "123 456 3 234243 12" |
||
234243 |
234243 |
||
>max |
>max |
||
Input stream: 5 4 3 2 67 1 |
Input stream: 5 4 3 2 67 1 |
||
67</ |
67</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> ListOfValues$ = "13, 0, -6, 2, 37, -10, 12" |
||
PRINT "Maximum value = " ; FNmax(ListOfValues$) |
PRINT "Maximum value = " ; FNmax(ListOfValues$) |
||
END |
END |
||
Line 1,017: | Line 1,017: | ||
IF number > max THEN max = number |
IF number > max THEN max = number |
||
UNTIL index% = 0 |
UNTIL index% = 0 |
||
= max</ |
= max</syntaxhighlight> |
||
=={{header|bc}}== |
=={{header|bc}}== |
||
< |
<syntaxhighlight lang="bc">define m(a[], n) { |
||
auto m, i |
auto m, i |
||
Line 1,028: | Line 1,028: | ||
} |
} |
||
return(m) |
return(m) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
< |
<syntaxhighlight lang="befunge">001pv < |
||
>&:01g`#v_1+#^_01g.@ |
>&:01g`#v_1+#^_01g.@ |
||
^p10 <</ |
^p10 <</syntaxhighlight> |
||
Only works with positive integers. List must be terminated with -1. |
Only works with positive integers. List must be terminated with -1. |
||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
<lang |
<syntaxhighlight lang="bqn">Max ← ⌈´</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,048: | Line 1,048: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
When comparing two rational numbers, Bracmat compares numerically. In all other cases Bracmat compares lexically. |
When comparing two rational numbers, Bracmat compares numerically. In all other cases Bracmat compares lexically. |
||
<lang> ( biggest |
<syntaxhighlight lang="text"> ( biggest |
||
= max |
= max |
||
. !arg: |
. !arg: |
||
Line 1,061: | Line 1,061: | ||
biggest |
biggest |
||
$ (5 100000 -5 43756243978569758/13 3365864921428443 87512487957139516/27 3446) |
$ (5 100000 -5 43756243978569758/13 3365864921428443 87512487957139516/27 3446) |
||
)</ |
)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>1: mies |
<pre>1: mies |
||
Line 1,069: | Line 1,069: | ||
=={{header|Brat}}== |
=={{header|Brat}}== |
||
Arrays have a max function, but here's a manual implementation. |
Arrays have a max function, but here's a manual implementation. |
||
< |
<syntaxhighlight lang="brat">max = { list | |
||
list.reduce { n, max | |
list.reduce { n, max | |
||
true? n > max |
true? n > max |
||
Line 1,077: | Line 1,077: | ||
} |
} |
||
p max [3 4 1 2]</ |
p max [3 4 1 2]</syntaxhighlight> |
||
=={{header|Burlesque}}== |
=={{header|Burlesque}}== |
||
< |
<syntaxhighlight lang="burlesque"> |
||
blsq ) {88 99 77 66 55}>] |
blsq ) {88 99 77 66 55}>] |
||
99 |
99 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
This works well with floats. Replace with double, int or what-have-you before passing a different data type. |
This works well with floats. Replace with double, int or what-have-you before passing a different data type. |
||
< |
<syntaxhighlight lang="c">#include <assert.h> |
||
float max(unsigned int count, float values[]) { |
float max(unsigned int count, float values[]) { |
||
Line 1,098: | Line 1,098: | ||
} |
} |
||
return themax; |
return themax; |
||
}</ |
}</syntaxhighlight> |
||
The following macro can be used with any number and type of arguments, provided that the arguments are ''simple'', i.e. must not contain subexpressions where commas appear (this is because of the way the arguments are counted; the macro can be modified so that it is up to the caller to count the number of arguments passed). <!-- You might wanna look at the macro from here which can count the number of arguments without parsing commas: http://groups.google.com/group/comp.std.c/browse_thread/thread/77ee8c8f92e4a3fb/346fc464319b1ee5 --> |
The following macro can be used with any number and type of arguments, provided that the arguments are ''simple'', i.e. must not contain subexpressions where commas appear (this is because of the way the arguments are counted; the macro can be modified so that it is up to the caller to count the number of arguments passed). <!-- You might wanna look at the macro from here which can count the number of arguments without parsing commas: http://groups.google.com/group/comp.std.c/browse_thread/thread/77ee8c8f92e4a3fb/346fc464319b1ee5 --> |
||
{{works with|GCC}} |
{{works with|GCC}} |
||
< |
<syntaxhighlight lang="c">#include <stdarg.h> |
||
#define MAX(A,...) ({ inline __typeof__ (A) _max_(__typeof__ (A) a, ...) {\ |
#define MAX(A,...) ({ inline __typeof__ (A) _max_(__typeof__ (A) a, ...) {\ |
||
Line 1,116: | Line 1,116: | ||
}\ |
}\ |
||
_max_((A),__VA_ARGS__);\ |
_max_((A),__VA_ARGS__);\ |
||
})</ |
})</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
Line 1,122: | Line 1,122: | ||
C# already has a "Maximum Value" function. |
C# already has a "Maximum Value" function. |
||
< |
<syntaxhighlight lang="csharp">int[] values = new int[] {1,2,3,4,5,6,7,8,9,10}; |
||
int max = values.Max();</ |
int max = values.Max();</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
A simple wrapper around the standard library function <tt>max_element()</tt>. |
A simple wrapper around the standard library function <tt>max_element()</tt>. |
||
Requires C++17. |
Requires C++17. |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> //std::max_element |
||
#include <iterator> //std::begin and std::end |
#include <iterator> //std::begin and std::end |
||
#include <functional> //std::less |
#include <functional> //std::less |
||
Line 1,148: | Line 1,148: | ||
using std::begin; using std::end; |
using std::begin; using std::end; |
||
return max_value(begin(container), end(container), compare); |
return max_value(begin(container), end(container), compare); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|CFEngine}}== |
=={{header|CFEngine}}== |
||
Line 1,154: | Line 1,154: | ||
Note: CFEngine bundles are NOT functions, however they can behave in some ways that are similar to functions. |
Note: CFEngine bundles are NOT functions, however they can behave in some ways that are similar to functions. |
||
< |
<syntaxhighlight lang="cfengine3"> |
||
bundle agent __main__ |
bundle agent __main__ |
||
{ |
{ |
||
Line 1,190: | Line 1,190: | ||
bundle_return_value_index => "max"; |
bundle_return_value_index => "max"; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
Line 1,199: | Line 1,199: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
The Clojure.core function max returns the max of its arguments. |
The Clojure.core function max returns the max of its arguments. |
||
< |
<syntaxhighlight lang="clojure">(max 1 2 3 4) ; evaluates to 4 |
||
;; 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</ |
(apply max [1 2 3 4]) ; evaluates to 4</syntaxhighlight> |
||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">% This "maximum" procedure is fully general, as long as |
||
% the container type has an elements iterator and the |
% the container type has an elements iterator and the |
||
% data type is comparable. |
% data type is comparable. |
||
Line 1,242: | Line 1,242: | ||
rmax: real := maximum[sequence[real], real](reals) |
rmax: real := maximum[sequence[real], real](reals) |
||
stream$putl(po, "maximum real: " || real$unparse(rmax)) |
stream$putl(po, "maximum real: " || real$unparse(rmax)) |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,251: | Line 1,251: | ||
Only for lists of integers. |
Only for lists of integers. |
||
< |
<syntaxhighlight lang="cmake"># max(var [value1 value2...]) sets var to the maximum of a list of |
||
# integers. If list is empty, sets var to NO. |
# integers. If list is empty, sets var to NO. |
||
function(max var) |
function(max var) |
||
Line 1,269: | Line 1,269: | ||
set(list 33 11 44 22 66 55) |
set(list 33 11 44 22 66 55) |
||
max(maximum ${list}) |
max(maximum ${list}) |
||
message(STATUS "maximum of ${list} => ${maximum}")</ |
message(STATUS "maximum of ${list} => ${maximum}")</syntaxhighlight> |
||
<pre>-- maximum of 33;11;44;22;66;55 => 66</pre> |
<pre>-- maximum of 33;11;44;22;66;55 => 66</pre> |
||
Line 1,275: | Line 1,275: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
This is already built into the language for tables of numbers. |
This is already built into the language for tables of numbers. |
||
< |
<syntaxhighlight lang="cobol">DISPLAY FUNCTION MAX(nums (ALL))</syntaxhighlight> |
||
A sample implementation: |
A sample implementation: |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
FUNCTION-ID. greatest-elt. |
FUNCTION-ID. greatest-elt. |
||
Line 1,302: | Line 1,302: | ||
GOBACK |
GOBACK |
||
. |
. |
||
END FUNCTION greatest-elt.</ |
END FUNCTION greatest-elt.</syntaxhighlight> |
||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
< |
<syntaxhighlight lang="coffeescript"> |
||
# using Math library |
# using Math library |
||
max1 = (list) -> |
max1 = (list) -> |
||
Line 1,322: | Line 1,322: | ||
a = [0,1,2,5,4]; |
a = [0,1,2,5,4]; |
||
alert(max1(a)+". The answer is "+max2(a)); |
alert(max1(a)+". The answer is "+max2(a)); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ColdFusion}}== |
=={{header|ColdFusion}}== |
||
< |
<syntaxhighlight lang="cfm"> |
||
<Cfset theList = '1, 1000, 250, 13'> |
<Cfset theList = '1, 1000, 250, 13'> |
||
<Cfparam name="maxNum" default=0> |
<Cfparam name="maxNum" default=0> |
||
Line 1,332: | Line 1,332: | ||
</Cfloop> |
</Cfloop> |
||
<Cfoutput>#maxNum#</Cfoutput> |
<Cfoutput>#maxNum#</Cfoutput> |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="cfm"> |
||
<Cfset theList = '1, 1000, 250, 13'> |
<Cfset theList = '1, 1000, 250, 13'> |
||
<Cfset maxNum = ListFirst(ListSort(thelist, "numeric", "desc"))> |
<Cfset maxNum = ListFirst(ListSort(thelist, "numeric", "desc"))> |
||
<Cfoutput>#maxNum#</Cfoutput> |
<Cfoutput>#maxNum#</Cfoutput> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
The built-in Common Lisp function <tt>max</tt> takes the max of all its arguments. |
The built-in Common Lisp function <tt>max</tt> takes the max of all its arguments. |
||
< |
<syntaxhighlight lang="lisp">(max 1 2 3 4) |
||
(reduce #'max values) ; find max of a list |
(reduce #'max values) ; find max of a list |
||
(loop for x in values |
(loop for x in values |
||
maximize x) ; alternative way to find max of a list</ |
maximize x) ; alternative way to find max of a list</syntaxhighlight> |
||
=={{header|Component Pascal}}== |
=={{header|Component Pascal}}== |
||
BalckBox Component Builder |
BalckBox Component Builder |
||
< |
<syntaxhighlight lang="oberon2"> |
||
MODULE Operations; |
MODULE Operations; |
||
IMPORT StdLog,Args,Strings; |
IMPORT StdLog,Args,Strings; |
||
Line 1,383: | Line 1,383: | ||
END Operations. |
END Operations. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Execute: ^Q Operations..DoMax 23 12 3 45 34 54 84 ~<br/> |
Execute: ^Q Operations..DoMax 23 12 3 45 34 54 84 ~<br/> |
||
{{Out}} |
{{Out}} |
||
Line 1,391: | Line 1,391: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
<lang |
<syntaxhighlight lang="ruby">values.max</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
Line 1,397: | Line 1,397: | ||
D already has a "Maximum Element" function. |
D already has a "Maximum Element" function. |
||
< |
<syntaxhighlight lang="d">void main() |
||
{ |
{ |
||
import std.algorithm.searching : maxElement; |
import std.algorithm.searching : maxElement; |
||
Line 1,403: | Line 1,403: | ||
[9, 4, 3, 8, 5].maxElement.writeln; |
[9, 4, 3, 8, 5].maxElement.writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>9</pre> |
<pre>9</pre> |
||
=={{header|Dart}}== |
=={{header|Dart}}== |
||
< |
<syntaxhighlight lang="dart">/*This is a function which returns the greatest element in a list of numbers */ |
||
num findGreatestElement(List<num> list){ |
num findGreatestElement(List<num> list){ |
||
num greatestElement = list[0]; |
num greatestElement = list[0]; |
||
Line 1,423: | Line 1,423: | ||
return list.reduce(max); |
return list.reduce(max); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|dc}}== |
=={{header|dc}}== |
||
< |
<syntaxhighlight lang="dc">[sm llx] sg |
||
[lm p q] sq |
[lm p q] sq |
||
[d lm <u s_ z 0 =q llx] sl |
[d lm <u s_ z 0 =q llx] sl |
||
Line 1,437: | Line 1,437: | ||
_275.0 _111.19 0.0 _1234568.0 lp lp _1 * |
_275.0 _111.19 0.0 _1234568.0 lp lp _1 * |
||
lgx</ |
lgx</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,443: | Line 1,443: | ||
=={{header|DCL}}== |
=={{header|DCL}}== |
||
< |
<syntaxhighlight lang="dcl">$ list = "45,65,81,12,0,13,-56,123,-123,888,12,0" |
||
$ max = f$integer( f$element( 0, ",", list )) |
$ max = f$integer( f$element( 0, ",", list )) |
||
$ i = 1 |
$ i = 1 |
||
Line 1,454: | Line 1,454: | ||
$ goto loop |
$ goto loop |
||
$ done: |
$ done: |
||
$ show symbol max</ |
$ show symbol max</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>$ @greatest |
<pre>$ @greatest |
||
Line 1,460: | Line 1,460: | ||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program GElemLIst; |
program GElemLIst; |
||
{$IFNDEF FPC} |
{$IFNDEF FPC} |
||
Line 1,484: | Line 1,484: | ||
writeln(Math.MaxValue(fltArr)); |
writeln(Math.MaxValue(fltArr)); |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |
||
< |
<syntaxhighlight lang="dyalect">func max(xs) { |
||
var y |
var y |
||
for x in xs { |
for x in xs { |
||
Line 1,498: | Line 1,498: | ||
var xs = [1..10] |
var xs = [1..10] |
||
max(xs)</ |
max(xs)</syntaxhighlight> |
||
=={{header|Déjà Vu}}== |
=={{header|Déjà Vu}}== |
||
< |
<syntaxhighlight lang="dejavu">max lst: |
||
lst! 0 |
lst! 0 |
||
for item in copy lst: |
for item in copy lst: |
||
Line 1,507: | Line 1,507: | ||
item drop |
item drop |
||
!. max [ 10 300 999 9 ]</ |
!. max [ 10 300 999 9 ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>999</pre> |
<pre>999</pre> |
||
=={{header|Draco}}== |
=={{header|Draco}}== |
||
< |
<syntaxhighlight lang="draco">/* Find the greatest element in an array of ints */ |
||
proc nonrec max([*] int a) int: |
proc nonrec max([*] int a) int: |
||
int INT_MIN = ~((~0) >> 1); |
int INT_MIN = ~((~0) >> 1); |
||
Line 1,527: | Line 1,527: | ||
type arr = [8] int; |
type arr = [8] int; |
||
writeln("Maximum: ", max(arr(1,5,17,2,53,99,61,3))) |
writeln("Maximum: ", max(arr(1,5,17,2,53,99,61,3))) |
||
corp</ |
corp</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Maximum: 99</pre> |
<pre>Maximum: 99</pre> |
||
Line 1,535: | Line 1,535: | ||
This function works for any value which responds to <code>[http://wiki.erights.org/wiki/Category:Message_max/1 max/1]</code>: |
This function works for any value which responds to <code>[http://wiki.erights.org/wiki/Category:Message_max/1 max/1]</code>: |
||
< |
<syntaxhighlight lang="e">pragma.enable("accumulator") # non-finalized syntax feature |
||
def max([first] + rest) { |
def max([first] + rest) { |
||
return accum first for x in rest { _.max(x) } |
return accum first for x in rest { _.max(x) } |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="e">? max([1, 2, 3]) |
||
# value: 3</ |
# value: 3</syntaxhighlight> |
||
To require only the comparison protocol, one needs to write out the algorithm a little more explicitly: |
To require only the comparison protocol, one needs to write out the algorithm a little more explicitly: |
||
< |
<syntaxhighlight lang="e">def max([var bestSoFar] + rest) { |
||
for x ? (x > bestSoFar) in rest { |
for x ? (x > bestSoFar) in rest { |
||
bestSoFar := x |
bestSoFar := x |
||
} |
} |
||
return bestSoFar |
return bestSoFar |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="e">? max([1, 3, 2]) |
||
# value: 3 |
# value: 3 |
||
? max([[1].asSet(), [2].asSet(), [1, 2].asSet()]) |
? max([[1].asSet(), [2].asSet(), [1, 2].asSet()]) |
||
# value: [1, 2].asSet()</ |
# value: [1, 2].asSet()</syntaxhighlight> |
||
=={{header|EasyLang}}== |
=={{header|EasyLang}}== |
||
<lang>a[] = [ 2 9 4 3 8 5 ] |
<syntaxhighlight lang="text">a[] = [ 2 9 4 3 8 5 ] |
||
for e in a[] |
for e in a[] |
||
max = higher e max |
max = higher e max |
||
. |
. |
||
print max</ |
print max</syntaxhighlight> |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
;; a random length list of random values |
;; a random length list of random values |
||
(define L (map random (make-list (random 50) 100))) → L |
(define L (map random (make-list (random 50) 100))) → L |
||
Line 1,575: | Line 1,575: | ||
;; find max |
;; find max |
||
(apply max L) → 97 |
(apply max L) → 97 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ECL}}== |
=={{header|ECL}}== |
||
<syntaxhighlight lang="ecl"> |
|||
<lang ECL> |
|||
MaxVal(SET OF INTEGER s) := MAX(s); |
MaxVal(SET OF INTEGER s) := MAX(s); |
||
Line 1,585: | Line 1,585: | ||
SetVals := [4,8,16,2,1]; |
SetVals := [4,8,16,2,1]; |
||
MaxVal(SetVals) //returns 16; |
MaxVal(SetVals) //returns 16; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Efene}}== |
=={{header|Efene}}== |
||
< |
<syntaxhighlight lang="efene">list_max = fn ([Head:Rest]) { |
||
list_max(Rest, Head) |
list_max(Rest, Head) |
||
} |
} |
||
Line 1,612: | Line 1,612: | ||
io.format("~p~n", [list_max1([9, 4, 3, 8, 5])]) |
io.format("~p~n", [list_max1([9, 4, 3, 8, 5])]) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
The GREATEST_ELEMENT class: |
The GREATEST_ELEMENT class: |
||
< |
<syntaxhighlight lang="eiffel"> |
||
class |
class |
||
GREATEST_ELEMENT [G -> COMPARABLE] |
GREATEST_ELEMENT [G -> COMPARABLE] |
||
Line 1,657: | Line 1,657: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
A test application: |
A test application: |
||
< |
<syntaxhighlight lang="eiffel"> |
||
class |
class |
||
APPLICATION |
APPLICATION |
||
Line 1,680: | Line 1,680: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ela}}== |
=={{header|Ela}}== |
||
< |
<syntaxhighlight lang="ela">open list |
||
findBy p (x::xs) = foldl (\x y | p x y -> x | else -> y) x xs |
findBy p (x::xs) = foldl (\x y | p x y -> x | else -> y) x xs |
||
maximum = findBy (>) |
maximum = findBy (>) |
||
maximum [1..10]</ |
maximum [1..10]</syntaxhighlight> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
< |
<syntaxhighlight lang="elena">import extensions; |
||
extension op |
extension op |
||
Line 1,722: | Line 1,722: | ||
{ |
{ |
||
console.printLine(new int[]{1,2,3,4,20,10,9,8}.Maximal) |
console.printLine(new int[]{1,2,3,4,20,10,9,8}.Maximal) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,729: | Line 1,729: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">iex(1)> Enum.max([3,1,4,1,5,9,2,6,5,3]) |
||
9</ |
9</syntaxhighlight> |
||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun find-maximum (items) |
||
(let (max) |
(let (max) |
||
(dolist (item items) |
(dolist (item items) |
||
Line 1,741: | Line 1,741: | ||
max)) |
max)) |
||
(find-maximum '(2 7 5)) ;=> 7</ |
(find-maximum '(2 7 5)) ;=> 7</syntaxhighlight> |
||
Built-in: |
Built-in: |
||
< |
<syntaxhighlight lang="lisp">(max '(2 7 5)) ;=> 7</syntaxhighlight> |
||
{{libheader|cl-lib}} |
{{libheader|cl-lib}} |
||
< |
<syntaxhighlight lang="lisp">(cl-loop for el in '(2 7 5) maximize el) ;=> 7 |
||
(cl-reduce #'max '(2 7 5)) ;=> 7</ |
(cl-reduce #'max '(2 7 5)) ;=> 7</syntaxhighlight> |
||
{{libheader|seq.el}} |
{{libheader|seq.el}} |
||
< |
<syntaxhighlight lang="lisp">(seq-max '(2 7 5)) ;=> 7</syntaxhighlight> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
Builtin. Using it from the Erlang shell: |
Builtin. Using it from the Erlang shell: |
||
< |
<syntaxhighlight lang="erlang">>lists:max([9,4,3,8,5]). |
||
9</ |
9</syntaxhighlight> |
||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
PROGRAM MAXLIST |
PROGRAM MAXLIST |
||
Line 1,786: | Line 1,786: | ||
PRINT("Max list element is";MAX) |
PRINT("Max list element is";MAX) |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
Note: The limit of this program is string variable lenght (255 chars). The advantage is no array use. |
Note: The limit of this program is string variable lenght (255 chars). The advantage is no array use. |
||
=={{header|Euler Math Toolbox}}== |
=={{header|Euler Math Toolbox}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
>v=random(1,100); |
>v=random(1,100); |
||
>max(v) |
>max(v) |
||
0.997492478596 |
0.997492478596 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
===Applying a function to each element of an array=== |
===Applying a function to each element of an array=== |
||
< |
<syntaxhighlight lang="euphoria">function aeval( sequence sArr, integer id ) |
||
for i = 1 to length( sArr ) do |
for i = 1 to length( sArr ) do |
||
sArr[ i ] = call_func( id, { sArr[ i ] } ) |
sArr[ i ] = call_func( id, { sArr[ i ] } ) |
||
Line 1,823: | Line 1,823: | ||
biggun = "ant" |
biggun = "ant" |
||
a = aeval( s, routine_id("biggest") ) |
a = aeval( s, routine_id("biggest") ) |
||
printf( 1, "%s\n", {biggun} )</ |
printf( 1, "%s\n", {biggun} )</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,831: | Line 1,831: | ||
===More trivial example=== |
===More trivial example=== |
||
< |
<syntaxhighlight lang="euphoria">function get_biggest(sequence s) |
||
object biggun |
object biggun |
||
biggun = s[1] |
biggun = s[1] |
||
Line 1,846: | Line 1,846: | ||
constant animals = {"ant", "antelope", "dog", "cat", "cow", "wolf", "wolverine", "aardvark"} |
constant animals = {"ant", "antelope", "dog", "cat", "cow", "wolf", "wolverine", "aardvark"} |
||
printf(1,"%s\n",{get_biggest(animals)})</ |
printf(1,"%s\n",{get_biggest(animals)})</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,857: | Line 1,857: | ||
Use the function MAX |
Use the function MAX |
||
<syntaxhighlight lang="excel"> |
|||
<lang Excel> |
|||
=MAX(3;2;1;4;5;23;1;2) |
=MAX(3;2;1;4;5;23;1;2) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,869: | Line 1,869: | ||
I generate a list of 10 random numbers at runtime then use F#'s built in function to find the maximum value of the list. |
I generate a list of 10 random numbers at runtime then use F#'s built in function to find the maximum value of the list. |
||
< |
<syntaxhighlight lang="fsharp"> |
||
let N = System.Random() |
let N = System.Random() |
||
let G = List.init 10 (fun _->N.Next()) |
let G = List.init 10 (fun _->N.Next()) |
||
List.iter (printf "%d ") G |
List.iter (printf "%d ") G |
||
printfn "\nMax value of list is %d" (List.max G) |
printfn "\nMax value of list is %d" (List.max G) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,883: | Line 1,883: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
The following word is in factor's standard library. |
The following word is in factor's standard library. |
||
< |
<syntaxhighlight lang="factor">: supremum ( seq -- elt ) [ ] [ max ] map-reduce ;</syntaxhighlight> |
||
=={{header|Fancy}}== |
=={{header|Fancy}}== |
||
< |
<syntaxhighlight lang="fancy">[1,-2,2,4,6,-4,-1,5] max println # => 6</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
Line 1,892: | Line 1,892: | ||
Has a built-in method to get maximum from a list. |
Has a built-in method to get maximum from a list. |
||
< |
<syntaxhighlight lang="fantom"> |
||
class Greatest |
class Greatest |
||
{ |
{ |
||
Line 1,902: | Line 1,902: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: array-max ( addr len -- max ) |
||
dup 0= if nip exit then |
dup 0= if nip exit then |
||
over @ rot cell+ rot 1- |
over @ rot cell+ rot 1- |
||
cells bounds ?do i @ max cell +loop ; |
cells bounds ?do i @ max cell +loop ; |
||
: stack-max ( n ... m count -- max ) 1 ?do max loop ;</ |
: stack-max ( n ... m count -- max ) 1 ?do max loop ;</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 1,917: | Line 1,917: | ||
The intrinsic function <tt>maxval</tt> returns the maximum value of the elements in an integer or real array: |
The intrinsic function <tt>maxval</tt> returns the maximum value of the elements in an integer or real array: |
||
< |
<syntaxhighlight lang="fortran">program test_maxval |
||
integer,dimension(5),parameter :: x = [10,100,7,1,2] |
integer,dimension(5),parameter :: x = [10,100,7,1,2] |
||
Line 1,925: | Line 1,925: | ||
write(*,'(F5.1)') maxval(y) |
write(*,'(F5.1)') maxval(y) |
||
end program test_maxval</ |
end program test_maxval</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,935: | Line 1,935: | ||
The intrinsic function <tt>max</tt> accepts any number of arguments. |
The intrinsic function <tt>max</tt> accepts any number of arguments. |
||
The type of these arguments can be integer, real, character, string of characters or arrays of these. |
The type of these arguments can be integer, real, character, string of characters or arrays of these. |
||
< |
<syntaxhighlight lang="fortran">program test_max |
||
implicit none |
implicit none |
||
Line 1,956: | Line 1,956: | ||
& max (['abc', 'hig', 'fde'], ['ghi', 'efd', 'cab'], ['def', 'bca', 'igh']) |
& max (['abc', 'hig', 'fde'], ['ghi', 'efd', 'cab'], ['def', 'bca', 'igh']) |
||
end program test_max</ |
end program test_max</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,970: | Line 1,970: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Function MaxElement(a() As Double) As Double |
Function MaxElement(a() As Double) As Double |
||
Line 1,994: | Line 1,994: | ||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep |
Sleep |
||
</syntaxhighlight> |
|||
</lang> |
|||
Example of use : |
Example of use : |
||
Line 2,009: | Line 2,009: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="frink"> |
||
println[max[[1,2,3,5,10,20]]] |
println[max[[1,2,3,5,10,20]]] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
Using the pre-defined function <code>max</code>: |
Using the pre-defined function <code>max</code>: |
||
< |
<syntaxhighlight lang="funl">println( max([1,2,3,-1,0]) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,022: | Line 2,022: | ||
It can be implemented as: |
It can be implemented as: |
||
< |
<syntaxhighlight lang="funl">def |
||
maximum( xs ) = |
maximum( xs ) = |
||
def |
def |
||
Line 2,033: | Line 2,033: | ||
foldl1( _, [] ) = error( "foldl1: empty list" ) |
foldl1( _, [] ) = error( "foldl1: empty list" ) |
||
foldl1( max, xs )</ |
foldl1( max, xs )</syntaxhighlight> |
||
=={{header|Futhark}}== |
=={{header|Futhark}}== |
||
This parallel formulation exploits the fact that negative infinity constitutes a neutral element for the maximum operator. |
This parallel formulation exploits the fact that negative infinity constitutes a neutral element for the maximum operator. |
||
< |
<syntaxhighlight lang="futhark"> |
||
let main (xs: []f64) = reduce f64.max (-f64.inf) xs |
let main (xs: []f64) = reduce f64.max (-f64.inf) xs |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 2,046: | Line 2,046: | ||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
The function fn GreatestElementInList() will sort and return the greatest element of any list that supports the @"compare:" operator. |
The function fn GreatestElementInList() will sort and return the greatest element of any list that supports the @"compare:" operator. |
||
< |
<syntaxhighlight lang="futurebasic"> |
||
include "NSLog.incl" |
include "NSLog.incl" |
||
Line 2,070: | Line 2,070: | ||
HandleEvents |
HandleEvents |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
<pre style="font-size: 13px"> |
<pre style="font-size: 13px"> |
||
Line 2,089: | Line 2,089: | ||
=={{header|GW-BASIC}}== |
=={{header|GW-BASIC}}== |
||
< |
<syntaxhighlight lang="gwbasic">10 INPUT "How many items? ", N% |
||
20 DIM ARR(N%) |
20 DIM ARR(N%) |
||
30 FOR I% = 0 TO N%-1 |
30 FOR I% = 0 TO N%-1 |
||
Line 2,100: | Line 2,100: | ||
100 NEXT I% |
100 NEXT I% |
||
110 PRINT "The maximum value was ";CHAMP;" at index ";INDEX;"." |
110 PRINT "The maximum value was ";CHAMP;" at index ";INDEX;"." |
||
120 END</ |
120 END</syntaxhighlight> |
||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang="gap"># Built-in |
||
L := List([1 .. 100], n -> Random(1, 10)); |
L := List([1 .. 100], n -> Random(1, 10)); |
||
MaximumList(L); |
MaximumList(L); |
||
# 10</ |
# 10</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
;Library |
;Library |
||
::< |
::<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 2,123: | Line 2,123: | ||
fmt.Println(floats.Max([]float64{63, 70, 37, 34, 83, 27, 19, 97, 9, 17})) // prt 97 |
fmt.Println(floats.Max([]float64{63, 70, 37, 34, 83, 27, 19, 97, 9, 17})) // prt 97 |
||
fmt.Println(floats.Min([]float64{63, 70, 37, 34, 83, 27, 19, 97, 9, 17})) // prt 9 |
fmt.Println(floats.Min([]float64{63, 70, 37, 34, 83, 27, 19, 97, 9, 17})) // prt 9 |
||
}</ |
}</syntaxhighlight> |
||
::< |
::<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 2,148: | Line 2,148: | ||
fmt.Println("The biggest number is ", biggest) // prt 97 |
fmt.Println("The biggest number is ", biggest) // prt 97 |
||
fmt.Println("The smallest number is ", smallest) // prt 9 |
fmt.Println("The smallest number is ", smallest) // prt 9 |
||
}</ |
}</syntaxhighlight> |
||
;List |
;List |
||
The task title says list. This solution uses a Go slice as a list. |
The task title says list. This solution uses a Go slice as a list. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 2,188: | Line 2,188: | ||
fmt.Println("empty list. no maximum.") |
fmt.Println("empty list. no maximum.") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
;Set |
;Set |
||
The task description says set. This solution uses a Go map as a set. |
The task description says set. This solution uses a Go map as a set. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 2,254: | Line 2,254: | ||
fmt.Println("no largest, empty set") |
fmt.Println("no largest, empty set") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Golfscript}}== |
=={{header|Golfscript}}== |
||
< |
<syntaxhighlight lang="golfscript">{$-1=}:max; |
||
[1 4 8 42 6 3]max # Example usage</ |
[1 4 8 42 6 3]max # Example usage</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">println ([2,4,0,3,1,2,-12].max())</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 2,268: | Line 2,268: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
The built-in Haskell function <tt>maximum</tt> returns a maximum based on default comparison between members of an ordered type. |
The built-in Haskell function <tt>maximum</tt> returns a maximum based on default comparison between members of an ordered type. |
||
< |
<syntaxhighlight lang="haskell">my_max = maximum</syntaxhighlight> |
||
It can alternately be defined as a "fold" on the built-in two-argument <tt>max</tt> function. |
It can alternately be defined as a "fold" on the built-in two-argument <tt>max</tt> function. |
||
< |
<syntaxhighlight lang="haskell">my_max = foldl1 max</syntaxhighlight> |
||
More generally, '''maximum''' is a special case of '''maximumBy''', which allows us to define or supply our own comparison function, and define the particular type of maximum that we need: |
More generally, '''maximum''' is a special case of '''maximumBy''', which allows us to define or supply our own comparison function, and define the particular type of maximum that we need: |
||
< |
<syntaxhighlight lang="haskell">import Data.List (maximumBy) |
||
import Data.Ord (comparing) |
import Data.Ord (comparing) |
||
Line 2,280: | Line 2,280: | ||
main :: IO () |
main :: IO () |
||
main = print $ maximumBy (comparing length) wds</ |
main = print $ maximumBy (comparing length) wds</syntaxhighlight> |
||
As a fold, maximumBy could be defined along the lines of: |
As a fold, maximumBy could be defined along the lines of: |
||
< |
<syntaxhighlight lang="haskell">maximumBy |
||
:: Foldable t |
:: Foldable t |
||
=> (a -> a -> Ordering) -> t a -> a |
=> (a -> a -> Ordering) -> t a -> a |
||
Line 2,292: | Line 2,292: | ||
GT -> x |
GT -> x |
||
_ -> y |
_ -> y |
||
in foldr1 max_</ |
in foldr1 max_</syntaxhighlight> |
||
=={{header|hexiscript}}== |
=={{header|hexiscript}}== |
||
< |
<syntaxhighlight lang="hexiscript">fun greatest a |
||
let l len a |
let l len a |
||
let max a[0] |
let max a[0] |
||
Line 2,304: | Line 2,304: | ||
endfor |
endfor |
||
return max |
return max |
||
endfun</ |
endfun</syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest"> |
||
max_value = MAX( -123, 234.56, 345.678, -456E3, -455) ! built-in function MAX(...) |
max_value = MAX( -123, 234.56, 345.678, -456E3, -455) ! built-in function MAX(...) |
||
Line 2,330: | Line 2,330: | ||
! max_value=345.678; values_found=30; values(1)=-123; values(2)=234.56; values(3)=345.678; values(4)=-456E3; values(5)=-455; values(6)=1; values(7)=2; values(8)=1; values(9)=0; values(10)=0; ...truncated |
! max_value=345.678; values_found=30; values(1)=-123; values(2)=234.56; values(3)=345.678; values(4)=-456E3; values(5)=-455; values(6)=1; values(7)=2; values(8)=1; values(9)=0; values(10)=0; ...truncated |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Hoon}}== |
=={{header|Hoon}}== |
||
< |
<syntaxhighlight lang="hoon">:- %say |
||
|= [^ [a=(list ,@) ~] ~] |
|= [^ [a=(list ,@) ~] ~] |
||
:- %noun |
:- %noun |
||
(snag 0 (sort a gte))</ |
(snag 0 (sort a gte))</syntaxhighlight> |
||
Usage: Add to a file gen/max.hoon |
Usage: Add to a file gen/max.hoon |
||
<pre> |
<pre> |
||
Line 2,344: | Line 2,344: | ||
=={{header|i}}== |
=={{header|i}}== |
||
< |
<syntaxhighlight lang="i">concept largest(l) { |
||
large = l[0] |
large = l[0] |
||
for element in l |
for element in l |
||
Line 2,356: | Line 2,356: | ||
software { |
software { |
||
print(largest([23, 1313, 21, 35757, 4, 434, 232, 2, 2342])) |
print(largest([23, 1313, 21, 35757, 4, 434, 232, 2, 2342])) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">procedure main() |
||
local l |
local l |
||
l := [7,8,6,9,4,5,2,3,1] |
l := [7,8,6,9,4,5,2,3,1] |
||
Line 2,370: | Line 2,370: | ||
every max <:= !l |
every max <:= !l |
||
return max |
return max |
||
end</ |
end</syntaxhighlight> |
||
=={{header|IS-BASIC}}== |
=={{header|IS-BASIC}}== |
||
< |
<syntaxhighlight lang="is-basic">1000 DEF FINDMAX(REF ARR) |
||
1010 LET MX=ARR(LBOUND(ARR)) |
1010 LET MX=ARR(LBOUND(ARR)) |
||
1020 FOR I=LBOUND(ARR)+1 TO UBOUND(ARR) |
1020 FOR I=LBOUND(ARR)+1 TO UBOUND(ARR) |
||
Line 2,379: | Line 2,379: | ||
1040 NEXT |
1040 NEXT |
||
1050 LET FINDMAX=MX |
1050 LET FINDMAX=MX |
||
1060 END DEF</ |
1060 END DEF</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution''':<lang |
'''Solution''':<syntaxhighlight lang="j"> >./</syntaxhighlight> |
||
'''Example''':< |
'''Example''':<syntaxhighlight lang="j"> >./ 1 2 3 2 1 |
||
3 |
3 |
||
>./'' NB. Maximum value of an empty list = identity element (or neutral) of max = -∞ |
>./'' NB. Maximum value of an empty list = identity element (or neutral) of max = -∞ |
||
__</ |
__</syntaxhighlight> |
||
(J's lists know how long they are.) |
(J's lists know how long they are.) |
||
=={{header|Janet}}== |
=={{header|Janet}}== |
||
< |
<syntaxhighlight lang="janet"> |
||
(def elems @[3 1 3 2]) |
(def elems @[3 1 3 2]) |
||
Line 2,399: | Line 2,399: | ||
# Unpack list as arguments to max function. |
# Unpack list as arguments to max function. |
||
(max ;elems) |
(max ;elems) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
The first function works with arrays of floats. Replace with arrays of double, int, or other primitive data type. |
The first function works with arrays of floats. Replace with arrays of double, int, or other primitive data type. |
||
< |
<syntaxhighlight lang="java">public static float max(float[] values) throws NoSuchElementException { |
||
if (values.length == 0) |
if (values.length == 0) |
||
throw new NoSuchElementException(); |
throw new NoSuchElementException(); |
||
Line 2,412: | Line 2,412: | ||
} |
} |
||
return themax; |
return themax; |
||
}</ |
}</syntaxhighlight> |
||
Optionally, if it is OK to rearrange the contents of the original array: |
Optionally, if it is OK to rearrange the contents of the original array: |
||
< |
<syntaxhighlight lang="java">public static float max(float[] values) throws NoSuchElementException { |
||
if (values.length == 0) |
if (values.length == 0) |
||
throw new NoSuchElementException(); |
throw new NoSuchElementException(); |
||
Arrays.sort(values);//sorts the values in ascending order |
Arrays.sort(values);//sorts the values in ascending order |
||
return values[values.length-1]; |
return values[values.length-1]; |
||
}</ |
}</syntaxhighlight> |
||
The following functions work with Lists or arrays of reference types, respectively. Note that the type is required to implement Comparable, to ensure we can compare them. For Lists, there is a utility method <tt>Collections.max()</tt> that already does this. For arrays, we can just use the <tt>Arrays.asList()</tt> wrapper to wrap it into a list and then use the function for lists. |
The following functions work with Lists or arrays of reference types, respectively. Note that the type is required to implement Comparable, to ensure we can compare them. For Lists, there is a utility method <tt>Collections.max()</tt> that already does this. For arrays, we can just use the <tt>Arrays.asList()</tt> wrapper to wrap it into a list and then use the function for lists. |
||
< |
<syntaxhighlight lang="java">import java.util.List; |
||
import java.util.Collections; |
import java.util.Collections; |
||
import java.util.Arrays; |
import java.util.Arrays; |
||
Line 2,433: | Line 2,433: | ||
public static <T extends Comparable<? super T>> T max(T[] values) { |
public static <T extends Comparable<? super T>> T max(T[] values) { |
||
return Collections.max(Arrays.asList(values)); |
return Collections.max(Arrays.asList(values)); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
===ES3-5=== |
===ES3-5=== |
||
< |
<syntaxhighlight lang="javascript">Math.max.apply(null, [ 0, 1, 2, 5, 4 ]); // 5</syntaxhighlight> |
||
===ES 5 maxima beyond simple numeric data types=== |
===ES 5 maxima beyond simple numeric data types=== |
||
Line 2,443: | Line 2,443: | ||
Math.max() serves well with simple numeric types, but for less restricted use we can write a generic '''maximumBy''' function which returns the maximum value from an array containing a series of any consistent data type, and which takes a type-specific comparison function as an argument. |
Math.max() serves well with simple numeric types, but for less restricted use we can write a generic '''maximumBy''' function which returns the maximum value from an array containing a series of any consistent data type, and which takes a type-specific comparison function as an argument. |
||
< |
<syntaxhighlight lang="javascript">(function () { |
||
// (a -> a -> Ordering) -> [a] -> a |
// (a -> a -> Ordering) -> [a] -> a |
||
Line 2,535: | Line 2,535: | ||
] |
] |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<syntaxhighlight lang="javascript">[ |
|||
<lang JavaScript>[ |
|||
"alpha", |
"alpha", |
||
"zeta", |
"zeta", |
||
Line 2,560: | Line 2,560: | ||
"population": 13.3 |
"population": 13.3 |
||
} |
} |
||
]</ |
]</syntaxhighlight> |
||
===ES6=== |
===ES6=== |
||
For, numbers, a method of the standard Math object: |
For, numbers, a method of the standard Math object: |
||
< |
<syntaxhighlight lang="javascript">Math.max(...[ 0, 1, 2, 5, 4 ]); // 5</syntaxhighlight> |
||
and for orderings defined over other datatypes: |
and for orderings defined over other datatypes: |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
"use strict"; |
"use strict"; |
||
Line 2,660: | Line 2,660: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>[ |
<pre>[ |
||
Line 2,672: | Line 2,672: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
jq defines a total ordering of all JSON entities, and the <tt>max</tt> filter can accordingly be used on any JSON array: |
jq defines a total ordering of all JSON entities, and the <tt>max</tt> filter can accordingly be used on any JSON array: |
||
< |
<syntaxhighlight lang="jq">[1, 3, 1.0] | max # => 3 |
||
[ {"a": 1}, {"a":3}, {"a":1.0}] | max # => {"a": 3}</ |
[ {"a": 1}, {"a":3}, {"a":1.0}] | max # => {"a": 3}</syntaxhighlight> |
||
Warning: both "[null]|max" and "[]|max" yield null. |
Warning: both "[null]|max" and "[]|max" yield null. |
||
Given a stream, s, of JSON values, the following filter will emit null if the stream is empty, or the maximum value otherwise: |
Given a stream, s, of JSON values, the following filter will emit null if the stream is empty, or the maximum value otherwise: |
||
< |
<syntaxhighlight lang="jq">reduce s as $x (null; if $x > . then $x else . end)</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Using the built-in <code>maximum</code> function: |
Using the built-in <code>maximum</code> function: |
||
< |
<syntaxhighlight lang="julia">julia> maximum([1,3,3,7]) |
||
7 |
7 |
||
Line 2,695: | Line 2,695: | ||
at In[138]:1 |
at In[138]:1 |
||
in maximum at abstractarray.jl:1591 |
in maximum at abstractarray.jl:1591 |
||
</syntaxhighlight> |
|||
</lang> |
|||
(Note that it throws an exception on an empty array.) |
(Note that it throws an exception on an empty array.) |
||
=={{header|K}}== |
=={{header|K}}== |
||
< |
<syntaxhighlight lang="k"> |/ 6 1 7 4 |
||
7</ |
7</syntaxhighlight> |
||
=={{header|Klingphix}}== |
=={{header|Klingphix}}== |
||
< |
<syntaxhighlight lang="klingphix">include ..\Utilitys.tlhy |
||
( "1" "1234" "62" "234" "12" "34" "6" ) |
( "1" "1234" "62" "234" "12" "34" "6" ) |
||
Line 2,715: | Line 2,715: | ||
"Numeric order: " print lmax ? |
"Numeric order: " print lmax ? |
||
" " input</ |
" " input</syntaxhighlight> |
||
=={{header|Klong}}== |
=={{header|Klong}}== |
||
< |
<syntaxhighlight lang="k">list::[ 1.0 2.3 1.1 5.0 3 2.8 2.01 3.14159 77 ] |
||
|/list |
|/list |
||
|/ [ 1.0 2.3 1.1 5.0 3 2.8 2.01 3.14159 66 ] |
|/ [ 1.0 2.3 1.1 5.0 3 2.8 2.01 3.14159 66 ] |
||
|/ 1.0,2.3,1.1,5.0,3,2.8,2.01,3.14159,55</ |
|/ 1.0,2.3,1.1,5.0,3,2.8,2.01,3.14159,55</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>77 |
<pre>77 |
||
Line 2,730: | Line 2,730: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Kotlin already has a 'max' function in its standard library so we use that: |
Kotlin already has a 'max' function in its standard library so we use that: |
||
< |
<syntaxhighlight lang="scala">// version 1.0.5-2 |
||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
print("Number of values to be input = ") |
print("Number of values to be input = ") |
||
Line 2,740: | Line 2,740: | ||
} |
} |
||
println("\nThe greatest element is ${array.max()}") |
println("\nThe greatest element is ${array.max()}") |
||
}</ |
}</syntaxhighlight> |
||
Example of use: |
Example of use: |
||
{{out}} |
{{out}} |
||
Line 2,754: | Line 2,754: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
<syntaxhighlight lang="scheme"> |
|||
<lang Scheme> |
|||
1) using the builtin primitive |
1) using the builtin primitive |
||
Line 2,777: | Line 2,777: | ||
{my-max {A.new 556 1 7344 4 7 52 22 55 88 122 55 99 1222 578}} |
{my-max {A.new 556 1 7344 4 7 52 22 55 88 122 55 99 1222 578}} |
||
-> 7344 |
-> 7344 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">define greatest(a::array) => { |
||
return (#a->sort&)->last |
return (#a->sort&)->last |
||
} |
} |
||
local(x = array(556,1,7344,4,7,52,22,55,88,122,55,99,1222,578)) |
local(x = array(556,1,7344,4,7,52,22,55,88,122,55,99,1222,578)) |
||
greatest(#x)</ |
greatest(#x)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,792: | Line 2,792: | ||
=={{header|LFE}}== |
=={{header|LFE}}== |
||
Builtin. Using it from the LFE shell: |
Builtin. Using it from the LFE shell: |
||
< |
<syntaxhighlight lang="lisp">>(: lists max '[9 4 3 8 5]) |
||
9</ |
9</syntaxhighlight> |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
< |
<syntaxhighlight lang="lb">aList$= "1 15 -5 6 39 1.5 14" |
||
maxVal = val(word$(aList$, 1)) |
maxVal = val(word$(aList$, 1)) |
||
Line 2,807: | Line 2,807: | ||
wend |
wend |
||
print "maxVal = ";maxVal</ |
print "maxVal = ";maxVal</syntaxhighlight> |
||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
< |
<syntaxhighlight lang="lingo">l = [1,7,5] |
||
put max(l) |
put max(l) |
||
-- 7</ |
-- 7</syntaxhighlight> |
||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
Max is built-in< |
Max is built-in<syntaxhighlight lang="livecode">put max(2,3,6,7,4,1)</syntaxhighlight> |
||
Result: 7 |
Result: 7 |
||
To be fair to the exercise, an alternative implementation could be< |
To be fair to the exercise, an alternative implementation could be<syntaxhighlight lang="livecode">function max2 lst |
||
local maxNum |
local maxNum |
||
put item 1 of lst into maxNum |
put item 1 of lst into maxNum |
||
Line 2,831: | Line 2,831: | ||
on mouseUp |
on mouseUp |
||
answer max2("1,2,5,6,7,4,3,2") |
answer max2("1,2,5,6,7,4,3,2") |
||
end mouseUp</ |
end mouseUp</syntaxhighlight> |
||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
Line 2,839: | Line 2,839: | ||
to use APPLY instead of REDUCE. The latter is good for associative |
to use APPLY instead of REDUCE. The latter is good for associative |
||
procedures that have been written to accept exactly two inputs: |
procedures that have been written to accept exactly two inputs: |
||
< |
<syntaxhighlight lang="logo">to max :a :b |
||
output ifelse :a > :b [:a] [:b] |
output ifelse :a > :b [:a] [:b] |
||
end |
end |
||
print reduce "max [...]</ |
print reduce "max [...]</syntaxhighlight> |
||
Alternatively, REDUCE can be used to write MAX as a procedure |
Alternatively, REDUCE can be used to write MAX as a procedure |
||
that accepts any number of inputs, as SUM does: |
that accepts any number of inputs, as SUM does: |
||
< |
<syntaxhighlight lang="logo">to max [:inputs] 2 |
||
if emptyp :inputs ~ |
if emptyp :inputs ~ |
||
[(throw "error [not enough inputs to max])] |
[(throw "error [not enough inputs to max])] |
||
output reduce [ifelse ?1 > ?2 [?1] [?2]] :inputs |
output reduce [ifelse ?1 > ?2 [?1] [?2]] :inputs |
||
end</ |
end</syntaxhighlight> =={{header|Logo}}== |
||
{{works with|UCB Logo}} |
{{works with|UCB Logo}} |
||
< |
<syntaxhighlight lang="logo">to bigger :a :b |
||
output ifelse [greater? :a :b] [:a] [:b] |
output ifelse [greater? :a :b] [:a] [:b] |
||
end |
end |
||
Line 2,859: | Line 2,859: | ||
to max :lst |
to max :lst |
||
output reduce "bigger :lst |
output reduce "bigger :lst |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Logtalk}}== |
=={{header|Logtalk}}== |
||
< |
<syntaxhighlight lang="logtalk"> |
||
max([X| Xs], Max) :- |
max([X| Xs], Max) :- |
||
max(Xs, X, Max). |
max(Xs, X, Max). |
||
Line 2,871: | Line 2,871: | ||
max(Xs, X, Max) |
max(Xs, X, Max) |
||
; max(Xs, Aux, Max) |
; max(Xs, Aux, Max) |
||
).</ |
).</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">-- Table to store values |
||
local values = {} |
local values = {} |
||
-- Read in the first number from stdin |
-- Read in the first number from stdin |
||
Line 2,887: | Line 2,887: | ||
-- Print the max |
-- Print the max |
||
print(math.max(unpack(values))) |
print(math.max(unpack(values))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module TestThis { |
Module TestThis { |
||
Print "Search a tuple type list (is an array also)" |
Print "Search a tuple type list (is an array also)" |
||
Line 2,967: | Line 2,967: | ||
} |
} |
||
TestThis |
TestThis |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
This is a built-in, polymorphic procedure in Maple. |
This is a built-in, polymorphic procedure in Maple. |
||
< |
<syntaxhighlight lang="maple">> max( { 1, 2, Pi, exp(1) } ); # set |
||
Pi |
Pi |
||
Line 2,981: | Line 2,981: | ||
> max( Array( [ 1, 2, Pi, exp(1) ] ) ); # Array |
> max( Array( [ 1, 2, Pi, exp(1) ] ) ); # Array |
||
Pi</ |
Pi</syntaxhighlight> |
||
For numeric data in (multi-dimensional) rtables, a particularly flexible and powerful method for finding the maximum (and many other things) is the use of "rtable_scanblock". The maximum of an Array is a built-in rtable_scanblock operation and can be found as follows. |
For numeric data in (multi-dimensional) rtables, a particularly flexible and powerful method for finding the maximum (and many other things) is the use of "rtable_scanblock". The maximum of an Array is a built-in rtable_scanblock operation and can be found as follows. |
||
< |
<syntaxhighlight lang="maple">> A := Array([1,2,4/5,3,11]): rtable_scanblock( A, [rtable_dims(A)], Maximum ); |
||
11</ |
11</syntaxhighlight> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
Input: |
Input: |
||
< |
<syntaxhighlight lang="mathematica">Max[1, 3, 3, 7] |
||
Max[Pi,E+2/5,17 Cos[6]/5,Sqrt[91/10]] |
Max[Pi,E+2/5,17 Cos[6]/5,Sqrt[91/10]] |
||
Max[1,6,Infinity] |
Max[1,6,Infinity] |
||
Max[]</ |
Max[]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> 7 |
<pre> 7 |
||
Line 2,998: | Line 2,998: | ||
-Infinity</pre> |
-Infinity</pre> |
||
Note that Max returns minus infinity if supplied with no arguments; as it should: |
Note that Max returns minus infinity if supplied with no arguments; as it should: |
||
< |
<syntaxhighlight lang="mathematica">Max[Max[],Max[a,b,c]] |
||
Max[Max[a],Max[b,c]] |
Max[Max[a],Max[b,c]] |
||
Max[Max[a,b],Max[c]] |
Max[Max[a,b],Max[c]] |
||
Max[Max[a,b,c],Max[]]</ |
Max[Max[a,b,c],Max[]]</syntaxhighlight> |
||
should all give the same results, therefore max[] should give -Infinity. If it WOULD give 0 strange things can happen: |
should all give the same results, therefore max[] should give -Infinity. If it WOULD give 0 strange things can happen: |
||
< |
<syntaxhighlight lang="mathematica">Max[Max[], Max[-4, -3]]</syntaxhighlight> |
||
WOULD give 0 instead of -3 |
WOULD give 0 instead of -3 |
||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
< |
<syntaxhighlight lang="matlab">function [maxValue] = findmax(setOfValues) |
||
maxValue = max(setOfValues);</ |
maxValue = max(setOfValues);</syntaxhighlight> |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">u : makelist(random(1000), 50)$ |
||
/* Three solutions */ |
/* Three solutions */ |
||
Line 3,018: | Line 3,018: | ||
apply(max, u); |
apply(max, u); |
||
lmax(u);</ |
lmax(u);</syntaxhighlight> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
MAXScript has a built-in function called amax(), which will return the maximum of an array or the values supplied to it. |
MAXScript has a built-in function called amax(), which will return the maximum of an array or the values supplied to it. |
||
The following custom function will return the maximum of the array supplied to it, or 'undefined' if an empty array is supplied. |
The following custom function will return the maximum of the array supplied to it, or 'undefined' if an empty array is supplied. |
||
< |
<syntaxhighlight lang="maxscript">fn MaxValue AnArray = |
||
( |
( |
||
if AnArray.count != 0 then |
if AnArray.count != 0 then |
||
Line 3,032: | Line 3,032: | ||
) |
) |
||
else undefined |
else undefined |
||
)</ |
)</syntaxhighlight> |
||
=={{header|Metafont}}== |
=={{header|Metafont}}== |
||
Line 3,038: | Line 3,038: | ||
The <code>max</code> macro (in the base set of macro for Metafont) accepts any number of arguments, and accepts both numerics (numbers), pairs (bidimensional vectors), and strings (not mixed). |
The <code>max</code> macro (in the base set of macro for Metafont) accepts any number of arguments, and accepts both numerics (numbers), pairs (bidimensional vectors), and strings (not mixed). |
||
< |
<syntaxhighlight lang="metafont">show max(4,5,20,1); |
||
show max((12,3), (10,10), (25,5)); |
show max((12,3), (10,10), (25,5)); |
||
show max("hello", "world", "Hello World");</ |
show max("hello", "world", "Hello World");</syntaxhighlight> |
||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.3}} |
{{works with|min|0.19.3}} |
||
< |
<syntaxhighlight lang="min">( |
||
'bool ;does the list have any elements? |
'bool ;does the list have any elements? |
||
(-inf ('> 'pop 'nip if) reduce) ;do if so |
(-inf ('> 'pop 'nip if) reduce) ;do if so |
||
({"empty seq" :error "Cannot find the maximum element of an empty sequence" :message} raise) ;do if not |
({"empty seq" :error "Cannot find the maximum element of an empty sequence" :message} raise) ;do if not |
||
if |
if |
||
) :seq-max</ |
) :seq-max</syntaxhighlight> |
||
=={{header|MiniScript}}== |
=={{header|MiniScript}}== |
||
There is a list.max function in the listUtil module, but if you're working in an environment without this module or just want to write it yourself, you might do it as follows. |
There is a list.max function in the listUtil module, but if you're working in an environment without this module or just want to write it yourself, you might do it as follows. |
||
< |
<syntaxhighlight lang="miniscript">list.max = function() |
||
if not self then return null |
if not self then return null |
||
result = self[0] |
result = self[0] |
||
Line 3,062: | Line 3,062: | ||
end function |
end function |
||
print [47, 11, 42, 102, 13].max</ |
print [47, 11, 42, 102, 13].max</syntaxhighlight> |
||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
<lang>П0 С/П x=0 07 ИП0 x<0 00 max БП 00</ |
<syntaxhighlight lang="text">П0 С/П x=0 07 ИП0 x<0 00 max БП 00</syntaxhighlight> |
||
or |
or |
||
<lang>П0 ИП0 С/П - x<0 01 Вx П0 БП 01</ |
<syntaxhighlight lang="text">П0 ИП0 С/П - x<0 01 Вx П0 БП 01</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
Line 3,075: | Line 3,075: | ||
We provide a generic Maximum implementation: |
We provide a generic Maximum implementation: |
||
< |
<syntaxhighlight lang="modula3">GENERIC INTERFACE Maximum(Elem); |
||
EXCEPTION Empty; |
EXCEPTION Empty; |
||
Line 3,081: | Line 3,081: | ||
PROCEDURE Max(READONLY a: ARRAY OF Elem.T): Elem.T RAISES {Empty}; |
PROCEDURE Max(READONLY a: ARRAY OF Elem.T): Elem.T RAISES {Empty}; |
||
END Maximum.</ |
END Maximum.</syntaxhighlight> |
||
< |
<syntaxhighlight lang="modula3">GENERIC MODULE Maximum(Elem); |
||
PROCEDURE Max(READONLY arr: ARRAY OF Elem.T): Elem.T RAISES {Empty} = |
PROCEDURE Max(READONLY arr: ARRAY OF Elem.T): Elem.T RAISES {Empty} = |
||
Line 3,100: | Line 3,100: | ||
BEGIN |
BEGIN |
||
END Maximum.</ |
END Maximum.</syntaxhighlight> |
||
<code>Elem</code> can be instantiated to any type (any type that can be compared with the '>' function). For convenience Modula-3 provides interfaces/modules for the built in types, like Integer, Real, LongReal, etc, which contain type definitions as well as properties specific to the type. |
<code>Elem</code> can be instantiated to any type (any type that can be compared with the '>' function). For convenience Modula-3 provides interfaces/modules for the built in types, like Integer, Real, LongReal, etc, which contain type definitions as well as properties specific to the type. |
||
To make a generic interface/module for a specific type, you must instantiate it: |
To make a generic interface/module for a specific type, you must instantiate it: |
||
< |
<syntaxhighlight lang="modula3">INTERFACE RealMax = Maximum(Real) END RealMax.</syntaxhighlight> |
||
< |
<syntaxhighlight lang="modula3">MODULE RealMax = Maximum(Real) END RealMax.</syntaxhighlight> |
||
Now we can import <code>RealMax</code> into our source and use the <code>Max</code> function: |
Now we can import <code>RealMax</code> into our source and use the <code>Max</code> function: |
||
< |
<syntaxhighlight lang="modula3">MODULE Main; |
||
IMPORT RealMax, IO, Fmt; |
IMPORT RealMax, IO, Fmt; |
||
Line 3,117: | Line 3,117: | ||
BEGIN |
BEGIN |
||
IO.Put(Fmt.Real(RealMax.Max(realarr)) & "\n"); |
IO.Put(Fmt.Real(RealMax.Max(realarr)) & "\n"); |
||
END Main.</ |
END Main.</syntaxhighlight> |
||
=={{header|MontiLang}}== |
=={{header|MontiLang}}== |
||
MontiLang has a builtin statement <code>MAX</code> which finds the maximum of the top two items on the stack. By looping through an array and pushing to the stack, the largest item in an array can be found. |
MontiLang has a builtin statement <code>MAX</code> which finds the maximum of the top two items on the stack. By looping through an array and pushing to the stack, the largest item in an array can be found. |
||
< |
<syntaxhighlight lang="montilang">2 5 3 12 9 9 56 2 ARR |
||
LEN VAR l . |
LEN VAR l . |
||
Line 3,132: | Line 3,132: | ||
FOR st |
FOR st |
||
MAX |
MAX |
||
ENDFOR PRINT</ |
ENDFOR PRINT</syntaxhighlight> |
||
Another way to do it. |
Another way to do it. |
||
< |
<syntaxhighlight lang="montilang">2 5 3 12 9 9 56 2 ARR |
||
print |
print |
||
LEN VAR l . |
LEN VAR l . |
||
Line 3,148: | Line 3,148: | ||
|Greatest number in the list: | out . print |
|Greatest number in the list: | out . print |
||
|Press ENTER to exit | input |
|Press ENTER to exit | input |
||
clear</ |
clear</syntaxhighlight> |
||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
<syntaxhighlight lang="mumps"> |
|||
<lang MUMPS> |
|||
MV(A,U) |
MV(A,U) |
||
;A is a list of values separated by the string U |
;A is a list of values separated by the string U |
||
Line 3,157: | Line 3,157: | ||
FOR I=1:1 SET T=$PIECE(A,U,I) QUIT:T="" S MAX=$SELECT(($DATA(MAX)=0):T,(MAX<T):T,(MAX>=T):MAX) |
FOR I=1:1 SET T=$PIECE(A,U,I) QUIT:T="" S MAX=$SELECT(($DATA(MAX)=0):T,(MAX<T):T,(MAX>=T):MAX) |
||
QUIT MAX |
QUIT MAX |
||
</syntaxhighlight> |
|||
</lang> |
|||
Usage: |
Usage: |
||
<pre> |
<pre> |
||
Line 3,169: | Line 3,169: | ||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
< |
<syntaxhighlight lang="nanoquery">def max(list) |
||
if len(list) = 0 |
if len(list) = 0 |
||
return null |
return null |
||
Line 3,181: | Line 3,181: | ||
end |
end |
||
return largest |
return largest |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Neko}}== |
=={{header|Neko}}== |
||
<syntaxhighlight lang="actionscript">/** |
|||
<lang ActionScript>/** |
|||
greatest element from a list (Neko Array) |
greatest element from a list (Neko Array) |
||
Tectonics: |
Tectonics: |
||
Line 3,207: | Line 3,207: | ||
$print(greatest($array(5, 1, 3, 5)), "\n"); |
$print(greatest($array(5, 1, 3, 5)), "\n"); |
||
$print(greatest($array("abc", "123", "zyx", "def")), "\n");</ |
$print(greatest($array("abc", "123", "zyx", "def")), "\n");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,216: | Line 3,216: | ||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
< |
<syntaxhighlight lang="nemerle">using System; |
||
using Nemerle.Collections; |
using Nemerle.Collections; |
||
using System.Linq; |
using System.Linq; |
||
Line 3,243: | Line 3,243: | ||
WriteLine($"letters.Max() = $(letters.Max())") |
WriteLine($"letters.Max() = $(letters.Max())") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 3,252: | Line 3,252: | ||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols binary |
options replace format comments java crossref savelog symbols binary |
||
Line 3,284: | Line 3,284: | ||
end n_ |
end n_ |
||
return dmax |
return dmax |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,292: | Line 3,292: | ||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang="newlisp">(max 1 2 3 5 2 3 4)</syntaxhighlight> |
||
< |
<syntaxhighlight lang="newlisp">(apply max '(1 2 3 5 2 3 4)) ; apply to list |
||
; Added by Nehal-Singhal 2018-05-29</ |
; Added by Nehal-Singhal 2018-05-29</syntaxhighlight> |
||
=={{header|Nial}}== |
=={{header|Nial}}== |
||
The behavior of multi-dimensional arrays is like J |
The behavior of multi-dimensional arrays is like J |
||
< |
<syntaxhighlight lang="nial">max 1 2 3 4 |
||
=4</ |
=4</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">echo max([2,3,4,5,6,1])</syntaxhighlight> |
||
{{out}}<pre>6</pre> |
{{out}}<pre>6</pre> |
||
Line 3,310: | Line 3,310: | ||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
{{Works with|oo2c Version 2}} |
{{Works with|oo2c Version 2}} |
||
< |
<syntaxhighlight lang="oberon2"> |
||
MODULE GreatestElement1; |
MODULE GreatestElement1; |
||
IMPORT |
IMPORT |
||
Line 3,349: | Line 3,349: | ||
Out.String("Max: ");Out.LongInt(max.value,4);Out.Ln |
Out.String("Max: ");Out.LongInt(max.value,4);Out.Ln |
||
END GreatestElement1. |
END GreatestElement1. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Simple version |
Simple version |
||
< |
<syntaxhighlight lang="oberon2"> |
||
MODULE GreatestElement2; |
MODULE GreatestElement2; |
||
IMPORT |
IMPORT |
||
Line 3,379: | Line 3,379: | ||
Out.String("Max: ");Out.LongInt(Max(a),4);Out.Ln |
Out.String("Max: ");Out.LongInt(Max(a),4);Out.Ln |
||
END GreatestElement2. |
END GreatestElement2. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}(in both programs): |
{{out}}(in both programs): |
||
<pre> |
<pre> |
||
Line 3,387: | Line 3,387: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
The language has a "Max" method for vectors. |
The language has a "Max" method for vectors. |
||
< |
<syntaxhighlight lang="objeck"> |
||
values := IntVector->New([4, 1, 42, 5]); |
values := IntVector->New([4, 1, 42, 5]); |
||
values->Max()->PrintLine(); |
values->Max()->PrintLine(); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
Line 3,410: | Line 3,410: | ||
< |
<syntaxhighlight lang="objc">#import <Foundation/Foundation.h> |
||
@interface NSArray (WithMaximum) |
@interface NSArray (WithMaximum) |
||
Line 3,432: | Line 3,432: | ||
return maybeMax; |
return maybeMax; |
||
} |
} |
||
@end</ |
@end</syntaxhighlight> |
||
This example mixes integers with a double value, just to show that |
This example mixes integers with a double value, just to show that |
||
everything is fine until they are NSNumber. |
everything is fine until they are NSNumber. |
||
< |
<syntaxhighlight lang="objc">int main() |
||
{ |
{ |
||
@autoreleasepool { |
@autoreleasepool { |
||
Line 3,445: | Line 3,445: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let my_max = function |
||
[] -> invalid_arg "empty list" |
[] -> invalid_arg "empty list" |
||
| x::xs -> List.fold_left max x xs</ |
| x::xs -> List.fold_left max x xs</syntaxhighlight> |
||
# my_max [4;3;5;9;2;3] ;; |
# my_max [4;3;5;9;2;3] ;; |
||
Line 3,459: | Line 3,459: | ||
Octave's <code>max</code> accepts a vector (and can return also the index of the maximum value in the vector) |
Octave's <code>max</code> accepts a vector (and can return also the index of the maximum value in the vector) |
||
< |
<syntaxhighlight lang="octave">m = max( [1,2,3,20,10,9,8] ); % m = 20 |
||
[m, im] = max( [1,2,3,20,10,9,8] ); % im = 4</ |
[m, im] = max( [1,2,3,20,10,9,8] ); % im = 4</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">[1, 2.3, 5.6, 1, 3, 4 ] reduce(#max)</syntaxhighlight> |
||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
Basics: |
Basics: |
||
< |
<syntaxhighlight lang="scheme"> |
||
; builtin function |
; builtin function |
||
(max 1 2 3 4 5) ; 5 |
(max 1 2 3 4 5) ; 5 |
||
Line 3,484: | Line 3,484: | ||
(if (less? a b) b a)) |
(if (less? a b) b a)) |
||
(car x) x)) ; 5 |
(car x) x)) ; 5 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
===version=== |
===version=== |
||
<syntaxhighlight lang="oorexx"> |
|||
<lang ooRexx> |
|||
-- routine that will work with any ordered collection or sets and bags containing numbers. |
-- routine that will work with any ordered collection or sets and bags containing numbers. |
||
::routine listMax |
::routine listMax |
||
Line 3,504: | Line 3,504: | ||
return largest |
return largest |
||
</syntaxhighlight> |
|||
</lang> |
|||
===version 2 works with any strings=== |
===version 2 works with any strings=== |
||
<lang>/* REXX *************************************************************** |
<syntaxhighlight lang="text">/* REXX *************************************************************** |
||
* 30.07.2013 Walter Pachl as for REXX |
* 30.07.2013 Walter Pachl as for REXX |
||
**********************************************************************/ |
**********************************************************************/ |
||
Line 3,524: | Line 3,524: | ||
largest = item |
largest = item |
||
end |
end |
||
return largest</ |
return largest</syntaxhighlight> |
||
=={{header|OxygenBasic}}== |
=={{header|OxygenBasic}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
'Works on any list with element types which support '>' comparisons |
'Works on any list with element types which support '>' comparisons |
||
Line 3,550: | Line 3,550: | ||
double m=max(d,5) |
double m=max(d,5) |
||
print "greatest element of d(): " m '5.5 |
print "greatest element of d(): " m '5.5 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">declare |
||
fun {Maximum X|Xr} %% pattern-match on argument to make sure the list is not empty |
fun {Maximum X|Xr} %% pattern-match on argument to make sure the list is not empty |
||
{FoldL Xr Value.max X} %% fold the binary function Value.max over the list |
{FoldL Xr Value.max X} %% fold the binary function Value.max over the list |
||
end |
end |
||
in |
in |
||
{Show {Maximum [1 2 3 4 3]}}</ |
{Show {Maximum [1 2 3 4 3]}}</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
<lang |
<syntaxhighlight lang="parigp">vecmax(v)</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 3,567: | Line 3,567: | ||
{{works with | Free Pascal}} |
{{works with | Free Pascal}} |
||
or try this, for it shows the according position |
or try this, for it shows the according position |
||
< |
<syntaxhighlight lang="pascal">program GElemLIst; |
||
{$IFNDEF FPC} |
{$IFNDEF FPC} |
||
{$Apptype Console} |
{$Apptype Console} |
||
Line 3,667: | Line 3,667: | ||
Ergflt := FindMaxflt(fltArr); |
Ergflt := FindMaxflt(fltArr); |
||
writeln('FindMaxFlt ',Ergflt.mpMax:0:4,' @ ',Ergflt.mpPos); |
writeln('FindMaxFlt ',Ergflt.mpMax:0:4,' @ ',Ergflt.mpPos); |
||
end.</ |
end.</syntaxhighlight> |
||
Out, because of the searchdirection Position of FindMaxFlt is below FindMaxInt |
Out, because of the searchdirection Position of FindMaxFlt is below FindMaxInt |
||
Line 3,675: | Line 3,675: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">sub max { |
||
my $max = shift; |
my $max = shift; |
||
for (@_) { $max = $_ if $_ > $max } |
for (@_) { $max = $_ if $_ > $max } |
||
return $max; |
return $max; |
||
}</ |
}</syntaxhighlight> |
||
It is already implemented in the module <tt>List::Util</tt>'s <tt>max()</tt> function: |
It is already implemented in the module <tt>List::Util</tt>'s <tt>max()</tt> function: |
||
< |
<syntaxhighlight lang="perl">use List::Util qw(max); |
||
max(@values);</ |
max(@values);</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1234</span><span style="color: #0000FF;">,</span><span style="color: #000000;">62</span><span style="color: #0000FF;">,</span><span style="color: #000000;">234</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">34</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">})</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1234</span><span style="color: #0000FF;">,</span><span style="color: #000000;">62</span><span style="color: #0000FF;">,</span><span style="color: #000000;">234</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">34</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"ant"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"antelope"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"dog"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"cat"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"cow"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"wolf"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"wolverine"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"aardvark"</span><span style="color: #0000FF;">})</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"ant"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"antelope"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"dog"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"cat"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"cow"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"wolf"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"wolverine"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"aardvark"</span><span style="color: #0000FF;">})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,699: | Line 3,699: | ||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
< |
<syntaxhighlight lang="phixmonti">"1" "1234" "62" "234" "12" "34" "6" stklen tolist |
||
dup "Alphabetic order: " print max print nl |
dup "Alphabetic order: " print max print nl |
||
Line 3,706: | Line 3,706: | ||
i get tonum i set |
i get tonum i set |
||
endfor |
endfor |
||
"Numeric order: " print max print</ |
"Numeric order: " print max print</syntaxhighlight> |
||
With syntactic sugar |
With syntactic sugar |
||
< |
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt |
||
( "1" "1234" "62" "234" "12" "34" "6" ) |
( "1" "1234" "62" "234" "12" "34" "6" ) |
||
Line 3,723: | Line 3,723: | ||
dup "Numeric order: " print max print |
dup "Numeric order: " print max print |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
The built-in PHP function <tt>max()</tt> already does this. |
The built-in PHP function <tt>max()</tt> already does this. |
||
<lang |
<syntaxhighlight lang="php">max($values)</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">: (max 2 4 1 3) # Return the maximal argument |
||
-> 4 |
-> 4 |
||
: (apply max (2 4 1 3)) # Apply to a list |
: (apply max (2 4 1 3)) # Apply to a list |
||
-> 4 |
-> 4 |
||
: (maxi abs (2 -4 -1 3)) # Maximum according to given function |
: (maxi abs (2 -4 -1 3)) # Maximum according to given function |
||
-> -4</ |
-> -4</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli"> |
||
maximum = A(lbound(A,1)); |
maximum = A(lbound(A,1)); |
||
do i = lbound(A,1)+1 to hbound(A,1); |
do i = lbound(A,1)+1 to hbound(A,1); |
||
if maximum < A(i) then maximum = A(i); |
if maximum < A(i) then maximum = A(i); |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PostScript}}== |
=={{header|PostScript}}== |
||
Line 3,750: | Line 3,750: | ||
{{works with|Ghostscript}} |
{{works with|Ghostscript}} |
||
< |
<syntaxhighlight lang="postscript">/findmax { |
||
dup 0 get exch % put the first element underneath the array |
dup 0 get exch % put the first element underneath the array |
||
{max} forall % replace it by the respective larger value if necessary |
{max} forall % replace it by the respective larger value if necessary |
||
} def</ |
} def</syntaxhighlight> |
||
If not using Ghostscript this gets a bit longer: |
If not using Ghostscript this gets a bit longer: |
||
< |
<syntaxhighlight lang="postscript">/findmax { |
||
dup 0 get exch % put the first element underneath the array |
dup 0 get exch % put the first element underneath the array |
||
{ |
{ |
||
Line 3,766: | Line 3,766: | ||
pop % remove it |
pop % remove it |
||
} forall |
} forall |
||
} def</ |
} def</syntaxhighlight> |
||
{{libheader|initlib}} |
{{libheader|initlib}} |
||
< |
<syntaxhighlight lang="postscript"> |
||
[1 2 3 4 5 4 3 2 1] uncons exch {max} fold |
[1 2 3 4 5 4 3 2 1] uncons exch {max} fold |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PowerBASIC}}== |
=={{header|PowerBASIC}}== |
||
< |
<syntaxhighlight lang="powerbasic">FUNCTION PBMAIN() |
||
DIM x AS LONG, y AS LONG, z AS LONG |
DIM x AS LONG, y AS LONG, z AS LONG |
||
RANDOMIZE TIMER |
RANDOMIZE TIMER |
||
Line 3,785: | Line 3,785: | ||
? STR$(z) & " was the highest value" |
? STR$(z) & " was the highest value" |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 3,794: | Line 3,794: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
The <code>Measure-Object</code> cmdlet in PowerShell already has this capability: |
The <code>Measure-Object</code> cmdlet in PowerShell already has this capability: |
||
< |
<syntaxhighlight lang="powershell">function Get-Maximum ($a) { |
||
return ($a | Measure-Object -Maximum).Maximum |
return ($a | Measure-Object -Maximum).Maximum |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
SWI-Prolog already knows max_list. |
SWI-Prolog already knows max_list. |
||
< |
<syntaxhighlight lang="prolog"> ?- max_list([1, 2, 10, 3, 0, 7, 9, 5], M). |
||
M = 10.</ |
M = 10.</syntaxhighlight> |
||
can be implemented like this: |
can be implemented like this: |
||
< |
<syntaxhighlight lang="prolog">max_list(L, V) :- |
||
select(V, L, R), \+((member(X, R), X > V)). |
select(V, L, R), \+((member(X, R), X > V)). |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure.f Max (Array a.f(1)) |
||
Protected last, i, ret.f |
Protected last, i, ret.f |
||
Line 3,822: | Line 3,822: | ||
ProcedureReturn ret |
ProcedureReturn ret |
||
EndProcedure</ |
EndProcedure</syntaxhighlight> |
||
===PureBasic: another solution=== |
===PureBasic: another solution=== |
||
< |
<syntaxhighlight lang="purebasic">Procedure.f maxelement(List tl.f()) |
||
ForEach tl() : mx.f=mx*Bool(mx>=tl())+tl()*Bool(mx<tl()) : Next |
ForEach tl() : mx.f=mx*Bool(mx>=tl())+tl()*Bool(mx<tl()) : Next |
||
ProcedureReturn mx |
ProcedureReturn mx |
||
Line 3,831: | Line 3,831: | ||
NewList testlist.f() : OpenConsole() |
NewList testlist.f() : OpenConsole() |
||
For i=0 To 99 : AddElement(testlist()) : testlist()=Sqr(Random(1000)) : Next |
For i=0 To 99 : AddElement(testlist()) : testlist()=Sqr(Random(1000)) : Next |
||
Print("Greatest element = "+StrF(maxelement(testlist()),8)) : Input()</ |
Print("Greatest element = "+StrF(maxelement(testlist()),8)) : Input()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Greatest element = 31.59113884</pre> |
<pre>Greatest element = 31.59113884</pre> |
||
Line 3,838: | Line 3,838: | ||
===Numeric values=== |
===Numeric values=== |
||
The built-in Python function <tt>max()</tt> already does this. |
The built-in Python function <tt>max()</tt> already does this. |
||
<lang |
<syntaxhighlight lang="python">max(values)</syntaxhighlight> |
||
Of course this assumes we have a list or tuple (or other sequence like object). (One can even find the ''max()'' or ''min()'' character of a string since that's treated as a sequence of characters and there are "less than" and "greater than" operations (object methods) associate with those characters). |
Of course this assumes we have a list or tuple (or other sequence like object). (One can even find the ''max()'' or ''min()'' character of a string since that's treated as a sequence of characters and there are "less than" and "greater than" operations (object methods) associate with those characters). |
||
Line 3,845: | Line 3,845: | ||
max(), (and min()), can take iterables and a key argument which takes a function that can transform each item into a type that we can compare, for example, if the stream were returning string representations of integers, one to a line, you could do |
max(), (and min()), can take iterables and a key argument which takes a function that can transform each item into a type that we can compare, for example, if the stream were returning string representations of integers, one to a line, you could do |
||
< |
<syntaxhighlight lang="python">>>> floatstrings = ['1\n', ' 2.3\n', '4.5e-1\n', '0.01e4\n', '-1.2'] |
||
>>> max(floatstrings, key = float) |
>>> max(floatstrings, key = float) |
||
'0.01e4\n' |
'0.01e4\n' |
||
>>></ |
>>></syntaxhighlight> |
||
Normally we would want the converted form as the maximum and we could just as easily write: |
Normally we would want the converted form as the maximum and we could just as easily write: |
||
< |
<syntaxhighlight lang="python">>>> max(float(x) for x in floatstrings) |
||
100.0 |
100.0 |
||
>>></ |
>>></syntaxhighlight> |
||
Or you can write your own functional version, of the maximum function, using reduce and lambda |
Or you can write your own functional version, of the maximum function, using reduce and lambda |
||
< |
<syntaxhighlight lang="python">>>> mylist = [47, 11, 42, 102, 13] |
||
>>> reduce(lambda a,b: a if (a > b) else b, mylist) |
>>> reduce(lambda a,b: a if (a > b) else b, mylist) |
||
102</ |
102</syntaxhighlight> |
||
And if you are being lazy but still want to meet this task's requirements : |
And if you are being lazy but still want to meet this task's requirements : |
||
< |
<syntaxhighlight lang="python"> |
||
max(list(map(int,input("").split(",")))) |
max(list(map(int,input("").split(",")))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Non-numeric values=== |
===Non-numeric values=== |
||
< |
<syntaxhighlight lang="python">'''Non-numeric maxima''' |
||
print( |
print( |
||
Line 3,894: | Line 3,894: | ||
{"k": "eta", "v": 32}, |
{"k": "eta", "v": 32}, |
||
{"k": "theta", "v": 16}], key=lambda x: x["v"])) |
{"k": "theta", "v": 16}], key=lambda x: x["v"])) |
||
)</ |
)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>max a-z: "zeta" |
<pre>max a-z: "zeta" |
||
Line 3,903: | Line 3,903: | ||
=={{header|Q}}== |
=={{header|Q}}== |
||
< |
<syntaxhighlight lang="q">q)l:2 9 3 8 4 7 |
||
q)max l |
q)max l |
||
9</ |
9</syntaxhighlight> |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery">[ behead swap witheach max ] is [max] ( [ --> n )</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">v <- c(1, 2, 100, 50, 0) |
||
print(max(v)) # 100</ |
print(max(v)) # 100</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
The "max" function it built in and takes an arbitrary amount of arguments. |
The "max" function it built in and takes an arbitrary amount of arguments. |
||
< |
<syntaxhighlight lang="racket">(max 12 9 8 17 1)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>17</pre> |
<pre>17</pre> |
||
To use with a list, there is <tt>apply</tt>: |
To use with a list, there is <tt>apply</tt>: |
||
< |
<syntaxhighlight lang="racket">(apply max '(12 9 8 17 1))</syntaxhighlight> |
||
However, if you want to write the function yourself: |
However, if you want to write the function yourself: |
||
< |
<syntaxhighlight lang="racket"> |
||
(define (my-max l) |
(define (my-max l) |
||
(define (max-h l greatest) |
(define (max-h l greatest) |
||
Line 3,931: | Line 3,931: | ||
[else (max-h (rest l) greatest)])) |
[else (max-h (rest l) greatest)])) |
||
(if (empty? l) empty (max-h l (first l)))) |
(if (empty? l) empty (max-h l (first l)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
or with a "for" loop: |
or with a "for" loop: |
||
< |
<syntaxhighlight lang="racket"> |
||
(define (my-max l) |
(define (my-max l) |
||
(for/fold ([max #f]) ([x l]) |
(for/fold ([max #f]) ([x l]) |
||
(if (and max (> max x)) max x))) |
(if (and max (> max x)) max x))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
The built-in function works with any type that defines ordering. |
The built-in function works with any type that defines ordering. |
||
<lang |
<syntaxhighlight lang="raku" line>say max 10, 4, 5, -2, 11; |
||
say max <zero one two three four five six seven eight nine>; |
say max <zero one two three four five six seven eight nine>; |
||
Line 3,949: | Line 3,949: | ||
my @list = flat(0..9,'A'..'H').roll((^60).pick).rotor(4,:partial)».join.words; |
my @list = flat(0..9,'A'..'H').roll((^60).pick).rotor(4,:partial)».join.words; |
||
say @list, ': ', max @list; |
say @list, ': ', max @list; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out|Sample output}} |
{{out|Sample output}} |
||
<pre>11 |
<pre>11 |
||
Line 3,956: | Line 3,956: | ||
=={{header|RapidQ}}== |
=={{header|RapidQ}}== |
||
< |
<syntaxhighlight lang="vb">functioni FindMax(...) as double |
||
dim x as integer |
dim x as integer |
||
Line 3,965: | Line 3,965: | ||
Print FindMax(50, 20, 65, 20, 105) |
Print FindMax(50, 20, 65, 20, 105) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Rascal}}== |
=={{header|Rascal}}== |
||
Rascal has a built-in function that gives the greatest element of a list |
Rascal has a built-in function that gives the greatest element of a list |
||
< |
<syntaxhighlight lang="rascal"> |
||
rascal>import List; |
rascal>import List; |
||
ok |
ok |
||
Line 3,975: | Line 3,975: | ||
rascal>max([1,2,3,4]); |
rascal>max([1,2,3,4]); |
||
int: 4 |
int: 4 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raven}}== |
=={{header|Raven}}== |
||
< |
<syntaxhighlight lang="raven">[ 1 2 3 4 ] max "%d\n" print</syntaxhighlight> |
||
{{out}} <pre>4</pre> |
{{out}} <pre>4</pre> |
||
'''Randomly generated list size and elements''' |
'''Randomly generated list size and elements''' |
||
< |
<syntaxhighlight lang="raven">100 choose as $cnt |
||
[ ] as $lst |
[ ] as $lst |
||
0 $cnt 1 range each drop 100 choose $lst push |
0 $cnt 1 range each drop 100 choose $lst push |
||
$lst print |
$lst print |
||
$lst max "max value: %d\n" print</ |
$lst max "max value: %d\n" print</syntaxhighlight> |
||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
< |
<syntaxhighlight lang="rebol">REBOL [ |
||
Title: "Maximum Value" |
Title: "Maximum Value" |
||
URL: http://rosettacode.org/wiki/Maximum_Value |
URL: http://rosettacode.org/wiki/Maximum_Value |
||
Line 4,002: | Line 4,002: | ||
print ["Max of" mold d: [5 4 3 2 1] "is" max d] |
print ["Max of" mold d: [5 4 3 2 1] "is" max d] |
||
print ["Max of" mold d: [-5 -4 -3 -2 -1] "is" max d]</ |
print ["Max of" mold d: [-5 -4 -3 -2 -1] "is" max d]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 4,009: | Line 4,009: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="red">Red [] |
||
list: [1 2 3 5 4] |
list: [1 2 3 5 4] |
||
print last sort list |
print last sort list |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
The numbers in the list may be any valid REXX number (integer, negative, floating point, etc.) |
The numbers in the list may be any valid REXX number (integer, negative, floating point, etc.) |
||
===using a list=== |
===using a list=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds the greatest element in a list (of the first 25 reversed primes).*/ |
||
$ = reverse(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97) |
$ = reverse(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97) |
||
say 'list of numbers = ' $ /*show the original list of numbers. */ |
say 'list of numbers = ' $ /*show the original list of numbers. */ |
||
Line 4,025: | Line 4,025: | ||
end /*j*/ |
end /*j*/ |
||
say /*stick a fork in it, we're all done. */ |
say /*stick a fork in it, we're all done. */ |
||
say 'the biggest value in a list of ' # " numbers is: " big</ |
say 'the biggest value in a list of ' # " numbers is: " big</syntaxhighlight> |
||
{{out|output|text= when using the default (internal) input:}} |
{{out|output|text= when using the default (internal) input:}} |
||
<pre> |
<pre> |
||
Line 4,034: | Line 4,034: | ||
===using an array=== |
===using an array=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds the greatest element in a list (of the first 25 reversed primes).*/ |
||
@.=; @.1 = 2; @.2 = 3; @.3 = 5; @.4 = 7; @.5 =11; @.6 =31; @.7 =71 |
@.=; @.1 = 2; @.2 = 3; @.3 = 5; @.4 = 7; @.5 =11; @.6 =31; @.7 =71 |
||
@.8 =91; @.9 =32; @.10=92; @.11=13; @.12=73; @.13=14; @.14=34 |
@.8 =91; @.9 =32; @.10=92; @.11=13; @.12=73; @.13=14; @.14=34 |
||
Line 4,044: | Line 4,044: | ||
end /*#*/ |
end /*#*/ |
||
/*stick a fork in it, we're all done. */ |
/*stick a fork in it, we're all done. */ |
||
say 'the biggest value in an array of ' #-1 " elements is: " big</ |
say 'the biggest value in an array of ' #-1 " elements is: " big</syntaxhighlight> |
||
{{out|output|text= when using the default (internal) input:}} |
{{out|output|text= when using the default (internal) input:}} |
||
<pre> |
<pre> |
||
Line 4,051: | Line 4,051: | ||
===using a list from the terminal=== |
===using a list from the terminal=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds the greatest element in a list of numbers entered at the terminal*/ |
||
say '────────────────── Please enter a list of numbers (separated by blanks or commas):' |
say '────────────────── Please enter a list of numbers (separated by blanks or commas):' |
||
parse pull $; #=words($) /*get a list of numbers from terminal. */ |
parse pull $; #=words($) /*get a list of numbers from terminal. */ |
||
Line 4,059: | Line 4,059: | ||
end /*j*/ |
end /*j*/ |
||
say /*stick a fork in it, we're all done. */ |
say /*stick a fork in it, we're all done. */ |
||
say '────────────────── The biggest value in the list of ' # " elements is: " big</ |
say '────────────────── The biggest value in the list of ' # " elements is: " big</syntaxhighlight> |
||
Programming note: the '''max''' BIF normalizes the number returned (eliding the leading superfluous zeroes). |
Programming note: the '''max''' BIF normalizes the number returned (eliding the leading superfluous zeroes). |
||
If this is undesirable, then the '''do''' loop (shown above) can be replaced with: |
If this is undesirable, then the '''do''' loop (shown above) can be replaced with: |
||
< |
<syntaxhighlight lang="rexx">···; do j=2 to #; _=word($,j) |
||
if _>big then big=_ |
if _>big then big=_ |
||
end /*j*/</ |
end /*j*/</syntaxhighlight> |
||
{{out|output|text= when using the input from the terminal via a user prompt:}} |
{{out|output|text= when using the input from the terminal via a user prompt:}} |
||
<pre> |
<pre> |
||
Line 4,075: | Line 4,075: | ||
===list of any strings=== |
===list of any strings=== |
||
< |
<syntaxhighlight lang="rexx">/* REXX *************************************************************** |
||
* If the list contains any character strings, the following will work |
* If the list contains any character strings, the following will work |
||
* Note the use of >> (instead of >) to avoid numeric comparison |
* Note the use of >> (instead of >) to avoid numeric comparison |
||
Line 4,093: | Line 4,093: | ||
max=word(l,i) |
max=word(l,i) |
||
End |
End |
||
Return max</ |
Return max</syntaxhighlight> |
||
{{out|output|text= when using an '''ASCII''' system:}} |
{{out|output|text= when using an '''ASCII''' system:}} |
||
<pre> |
<pre> |
||
Line 4,111: | Line 4,111: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring">aList = [1,2,4,5,10,6,7,8,9] |
||
see max(aList)</ |
see max(aList)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,120: | Line 4,120: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<tt>max</tt> is a method of all Enumerables |
<tt>max</tt> is a method of all Enumerables |
||
<lang |
<syntaxhighlight lang="ruby">values.max</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">list$= "1 12 -55 46 41 3.66 19" |
||
while word$(list$,i+1," ") <> "" |
while word$(list$,i+1," ") <> "" |
||
mx = max(mx,val(word$(list$,i+1," "))) |
mx = max(mx,val(word$(list$,i+1," "))) |
||
i = i + 1 |
i = i + 1 |
||
wend |
wend |
||
print mx</ |
print mx</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
This is built in functionality for everything that can be iterated over. It returns an Option<T>, meaning Some(e) if there are elements in the iterator and None if it is empty. |
This is built in functionality for everything that can be iterated over. It returns an Option<T>, meaning Some(e) if there are elements in the iterator and None if it is empty. |
||
< |
<syntaxhighlight lang="rust">fn main() { |
||
let nums = [1,2,39,34,20]; |
let nums = [1,2,39,34,20]; |
||
println!("{:?}", nums.iter().max()); |
println!("{:?}", nums.iter().max()); |
||
println!("{}", nums.iter().max().unwrap()); |
println!("{}", nums.iter().max().unwrap()); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,144: | Line 4,144: | ||
=={{header|S-lang}}== |
=={{header|S-lang}}== |
||
Starting w/an array, this is trivial: |
Starting w/an array, this is trivial: |
||
< |
<syntaxhighlight lang="s-lang">variable a = [5, -2, 0, 4, 666, 7]; |
||
print(max(a));</ |
print(max(a));</syntaxhighlight> |
||
output: |
output: |
||
Line 4,151: | Line 4,151: | ||
If a is a list instead of an array, then: |
If a is a list instead of an array, then: |
||
< |
<syntaxhighlight lang="s-lang">a = {5, -2, 0, 4, 666, 7}; |
||
print(max(list_to_array(a)));</ |
print(max(list_to_array(a)));</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{libheader|Scala}} |
{{libheader|Scala}} |
||
< |
<syntaxhighlight lang="scala">def noSweat(list: Int*) = list.max |
||
// Test |
// Test |
||
assert(noSweat(1, 3, 12, 7) == 12)</ |
assert(noSweat(1, 3, 12, 7) == 12)</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
The built-in Scheme function <tt>max</tt> takes the max of all its arguments. |
The built-in Scheme function <tt>max</tt> takes the max of all its arguments. |
||
< |
<syntaxhighlight lang="scheme">(max 1 2 3 4) |
||
(apply max values) ; find max of a list</ |
(apply max values) ; find max of a list</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func integer: max (in array integer: values) is func |
const func integer: max (in array integer: values) is func |
||
Line 4,185: | Line 4,185: | ||
begin |
begin |
||
writeln(max([] (1, 2, 6, 4, 3))); |
writeln(max([] (1, 2, 6, 4, 3))); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 4,195: | Line 4,195: | ||
Using ''reduceWith:'' it is very simple to find the maximum value among a collection. |
Using ''reduceWith:'' it is very simple to find the maximum value among a collection. |
||
< |
<syntaxhighlight lang="self">(1 & 2 & 3 & 4 & 20 & 10 & 9 & 8) asVector reduceWith: [:a :b | a max: b] "returns 20"</syntaxhighlight> |
||
Or, since it's "built-in", you can simply do: |
Or, since it's "built-in", you can simply do: |
||
< |
<syntaxhighlight lang="self">(1 & 2 & 3 & 4 & 20 & 10 & 9 & 8) asVector max. "returns 20"</syntaxhighlight> |
||
=={{header|SenseTalk}}== |
=={{header|SenseTalk}}== |
||
< |
<syntaxhighlight lang="sensetalk">put the max of (1, 5, 666, -1000, 3) |
||
put the highest value of [88,-2,6,55,103,0]</ |
put the highest value of [88,-2,6,55,103,0]</syntaxhighlight> |
||
Output: |
Output: |
||
< |
<syntaxhighlight lang="sensetalk">666 |
||
103</ |
103</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
''max'' method returns the greatest element in a list. It works only if the array's elements have the same type (e.g.: strings, numbers). |
''max'' method returns the greatest element in a list. It works only if the array's elements have the same type (e.g.: strings, numbers). |
||
<lang |
<syntaxhighlight lang="ruby">values.max;</syntaxhighlight> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
< |
<syntaxhighlight lang="slate">#(1 2 3 4 20 10 9 8) reduce: [| :a :b | a max: b]</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Using ''fold'' it is very simple to find the maximum value among a collection. |
Using ''fold'' it is very simple to find the maximum value among a collection. |
||
< |
<syntaxhighlight lang="smalltalk">#(1 2 3 4 20 10 9 8) fold: [:a :b | a max: b] "returns 20"</syntaxhighlight> |
||
Or, since it's "built-in", you can simply do: |
Or, since it's "built-in", you can simply do: |
||
Line 4,224: | Line 4,224: | ||
{{works with|Pharo|1.4}} |
{{works with|Pharo|1.4}} |
||
{{works with|Smalltalk/X}} |
{{works with|Smalltalk/X}} |
||
< |
<syntaxhighlight lang="smalltalk">#(1 2 3 4 20 10 9 8) max. "returns 20"</syntaxhighlight> |
||
using #inject:into: |
using #inject:into: |
||
< |
<syntaxhighlight lang="smalltalk"> |
||
| list | |
| list | |
||
list := #(1 2 3 4 20 10 9 8). |
list := #(1 2 3 4 20 10 9 8). |
||
list inject: (list at: 1) into: [ :number :each | |
list inject: (list at: 1) into: [ :number :each | |
||
number max: each ] |
number max: each ] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
< |
<syntaxhighlight lang="snobol4">while a = trim(input) :f(stop) |
||
max = gt(a,max) a :(while) |
max = gt(a,max) a :(while) |
||
stop output = max |
stop output = max |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
Comparisons are specific for each type. Here is a max function for a list of ints: |
Comparisons are specific for each type. Here is a max function for a list of ints: |
||
< |
<syntaxhighlight lang="sml">fun max_of_ints [] = raise Empty |
||
| max_of_ints (x::xs) = foldl Int.max x xs</ |
| max_of_ints (x::xs) = foldl Int.max x xs</syntaxhighlight> |
||
- max_of_ints [4,3,5,9,2,3]; |
- max_of_ints [4,3,5,9,2,3]; |
||
Line 4,252: | Line 4,252: | ||
Use the '''[https://www.stata.com/help.cgi?summarize summarize]''' command to compute the maximum value of a variable: |
Use the '''[https://www.stata.com/help.cgi?summarize summarize]''' command to compute the maximum value of a variable: |
||
< |
<syntaxhighlight lang="stata">qui sum x |
||
di r(max)</ |
di r(max)</syntaxhighlight> |
||
Mata has also several [https://www.stata.com/help.cgi?mf_minmax functions] to compute minimum or maximum of a vactor or matrix: |
Mata has also several [https://www.stata.com/help.cgi?mf_minmax functions] to compute minimum or maximum of a vactor or matrix: |
||
< |
<syntaxhighlight lang="stata">a = 1,5,3,4,2,7,9,8 |
||
max(a)</ |
max(a)</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
{{works with|Swift|2.x}} |
{{works with|Swift|2.x}} |
||
< |
<syntaxhighlight lang="swift">if let x = [4,3,5,9,2,3].maxElement() { |
||
print(x) // prints 9 |
print(x) // prints 9 |
||
}</ |
}</syntaxhighlight> |
||
{{works with|Swift|1.x}} |
{{works with|Swift|1.x}} |
||
< |
<syntaxhighlight lang="swift">let x = maxElement([4,3,5,9,2,3]) |
||
println(x) // prints 9</ |
println(x) // prints 9</syntaxhighlight> |
||
=={{header|Tailspin}}== |
=={{header|Tailspin}}== |
||
Simplest is to use the built-in collector |
Simplest is to use the built-in collector |
||
< |
<syntaxhighlight lang="tailspin"> |
||
[1, 5, 20, 3, 9, 7] ... -> ..=Max&{by: :(), select: :()} -> !OUT::write |
[1, 5, 20, 3, 9, 7] ... -> ..=Max&{by: :(), select: :()} -> !OUT::write |
||
// outputs 20 |
// outputs 20 |
||
Line 4,286: | Line 4,286: | ||
end result |
end result |
||
end Max |
end Max |
||
</syntaxhighlight> |
|||
</lang> |
|||
But here is how to find the max more manually/programmatically, it is pretty easy as well |
But here is how to find the max more manually/programmatically, it is pretty easy as well |
||
< |
<syntaxhighlight lang="tailspin"> |
||
templates max |
templates max |
||
@: $(1); |
@: $(1); |
||
Line 4,298: | Line 4,298: | ||
[1, 5, 20, 3, 9, 7] -> max -> !OUT::write |
[1, 5, 20, 3, 9, 7] -> max -> !OUT::write |
||
// outputs 20 |
// outputs 20 |
||
</syntaxhighlight> |
|||
</lang> |
|||
Can also be written as an inline templates |
Can also be written as an inline templates |
||
< |
<syntaxhighlight lang="tailspin"> |
||
[1, 5, 20, 3, 9, 7] -> \(@: $(1); $(2..last)... -> # $@ ! when <$@..> do @: $;\) -> !OUT::write |
[1, 5, 20, 3, 9, 7] -> \(@: $(1); $(2..last)... -> # $@ ! when <$@..> do @: $;\) -> !OUT::write |
||
// outputs 20 |
// outputs 20 |
||
</syntaxhighlight> |
|||
</lang> |
|||
Or we can do just the matching in an inline templates referencing the outer state |
Or we can do just the matching in an inline templates referencing the outer state |
||
< |
<syntaxhighlight lang="tailspin"> |
||
templates max |
templates max |
||
@: $(1); |
@: $(1); |
||
Line 4,314: | Line 4,314: | ||
[1, 5, 20, 3, 9, 7] -> max -> !OUT::write |
[1, 5, 20, 3, 9, 7] -> max -> !OUT::write |
||
// outputs 20 |
// outputs 20 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.5}} |
{{works with|Tcl|8.5}} |
||
Use the <code>{*}</code> expansion operator to substitute the list value with its constituent elements |
Use the <code>{*}</code> expansion operator to substitute the list value with its constituent elements |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
set values {4 3 2 7 8 9} |
set values {4 3 2 7 8 9} |
||
::tcl::mathfunc::max {*}$values ;# ==> 9</ |
::tcl::mathfunc::max {*}$values ;# ==> 9</syntaxhighlight> |
||
=={{header|TI-83 BASIC}}== |
=={{header|TI-83 BASIC}}== |
||
Line 4,333: | Line 4,333: | ||
=={{header|Transd}}== |
=={{header|Transd}}== |
||
< |
<syntaxhighlight lang="scheme">#lang transd |
||
MainModule: { |
MainModule: { |
||
Line 4,342: | Line 4,342: | ||
(textout (max-element-idx v)) |
(textout (max-element-idx v)) |
||
)) |
)) |
||
}</ |
}</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
11 274 3 |
11 274 3 |
||
Line 4,348: | Line 4,348: | ||
=={{header|Trith}}== |
=={{header|Trith}}== |
||
< |
<syntaxhighlight lang="trith">[1 -2 3.1415 0 42 7] [max] foldl1</syntaxhighlight> |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
LOOP n,list="2'4'0'3'1'2'-12" |
LOOP n,list="2'4'0'3'1'2'-12" |
||
Line 4,358: | Line 4,358: | ||
ENDLOOP |
ENDLOOP |
||
PRINT greatest |
PRINT greatest |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 4,366: | Line 4,366: | ||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
Since uBasic/4tH has a stack, it's only logical to use it. |
Since uBasic/4tH has a stack, it's only logical to use it. |
||
<lang>Push 13, 0, -6, 2, 37, -10, 12 ' Push values on the stack |
<syntaxhighlight lang="text">Push 13, 0, -6, 2, 37, -10, 12 ' Push values on the stack |
||
Print "Maximum value = " ; FUNC(_FNmax(7)) |
Print "Maximum value = " ; FUNC(_FNmax(7)) |
||
End ' We pushed seven values |
End ' We pushed seven values |
||
Line 4,379: | Line 4,379: | ||
If c@ > d@ THEN d@ = c@ ' Change maximum if required |
If c@ > d@ THEN d@ = c@ ' Change maximum if required |
||
Next |
Next |
||
Return (d@) ' Return the maximum</ |
Return (d@) ' Return the maximum</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 4,385: | Line 4,385: | ||
{{works with|pdksh}} |
{{works with|pdksh}} |
||
< |
<syntaxhighlight lang="bash">max() { |
||
local m=$1 |
local m=$1 |
||
shift |
shift |
||
Line 4,396: | Line 4,396: | ||
} |
} |
||
max 10 9 11 57 1 12</ |
max 10 9 11 57 1 12</syntaxhighlight> |
||
{{works with|Bourne Shell}} |
{{works with|Bourne Shell}} |
||
< |
<syntaxhighlight lang="bash">max() { |
||
m=$1 # Bourne Shell has no local command. |
m=$1 # Bourne Shell has no local command. |
||
shift |
shift |
||
Line 4,409: | Line 4,409: | ||
done |
done |
||
echo "$m" |
echo "$m" |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Ursa}}== |
=={{header|Ursa}}== |
||
The <code>max</code> function: |
The <code>max</code> function: |
||
< |
<syntaxhighlight lang="ursa">def max (int<> list) |
||
decl int max i |
decl int max i |
||
set max list<0> |
set max list<0> |
||
Line 4,424: | Line 4,424: | ||
return max |
return max |
||
end max</ |
end max</syntaxhighlight> |
||
In use: (assuming the function is in the file <code>max.u</code>) |
In use: (assuming the function is in the file <code>max.u</code>) |
||
Line 4,441: | Line 4,441: | ||
case it is used with <code>fleq</code>, the partial order relation on floating point |
case it is used with <code>fleq</code>, the partial order relation on floating point |
||
numbers. |
numbers. |
||
< |
<syntaxhighlight lang="ursala">#import flo |
||
#cast %e |
#cast %e |
||
example = fleq$^ <-1.,-2.,0.,5.,4.,6.,1.,-5.></ |
example = fleq$^ <-1.,-2.,0.,5.,4.,6.,1.,-5.></syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>6.000000e+00</pre> |
<pre>6.000000e+00</pre> |
||
Line 4,451: | Line 4,451: | ||
=={{header|V}}== |
=={{header|V}}== |
||
Assuming it is a list of positive numbers |
Assuming it is a list of positive numbers |
||
< |
<syntaxhighlight lang="v">[4 3 2 7 8 9] 0 [max] fold |
||
=9</ |
=9</syntaxhighlight> |
||
If it is not |
If it is not |
||
< |
<syntaxhighlight lang="v">[4 3 2 7 8 9] dup first [max] fold</syntaxhighlight> |
||
=9 |
=9 |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang="vb">Option Explicit |
||
Sub Main() |
Sub Main() |
||
Line 4,474: | Line 4,474: | ||
Next i |
Next i |
||
Max_VBA = temp |
Max_VBA = temp |
||
End Function</ |
End Function</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 4,480: | Line 4,480: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Function greatest_element(arr) |
Function greatest_element(arr) |
||
tmp_num = 0 |
tmp_num = 0 |
||
Line 4,494: | Line 4,494: | ||
WScript.Echo greatest_element(Array(1,2,3,44,5,6,8)) |
WScript.Echo greatest_element(Array(1,2,3,44,5,6,8)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 4,502: | Line 4,502: | ||
for numbers (not floats): |
for numbers (not floats): |
||
< |
<syntaxhighlight lang="vim">max([1, 3, 2])</syntaxhighlight> |
||
result: 3 |
result: 3 |
||
for strings (with configurable ignore-case): |
for strings (with configurable ignore-case): |
||
< |
<syntaxhighlight lang="vim">function! Max(list, ...) |
||
" {list} list of strings |
" {list} list of strings |
||
" {a:1} 'i': ignore case, 'I': match case, otherwise use 'ignorecase' option |
" {a:1} 'i': ignore case, 'I': match case, otherwise use 'ignorecase' option |
||
Line 4,524: | Line 4,524: | ||
endwhile |
endwhile |
||
return maxval |
return maxval |
||
endfunction</ |
endfunction</syntaxhighlight> |
||
=={{header|Visual Basic}}== |
=={{header|Visual Basic}}== |
||
< |
<syntaxhighlight lang="vb">Public Function ListMax(anArray()) |
||
'return the greatest element in array anArray |
'return the greatest element in array anArray |
||
'use LBound and UBound to find its length |
'use LBound and UBound to find its length |
||
Line 4,547: | Line 4,547: | ||
'print the greatest element |
'print the greatest element |
||
Debug.Print "Greatest element is"; ListMax(b()) |
Debug.Print "Greatest element is"; ListMax(b()) |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
Result: |
Result: |
||
Line 4,554: | Line 4,554: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
< |
<syntaxhighlight lang="vlang">fn max<T>(list []T) T { |
||
mut max := list[0] |
mut max := list[0] |
||
for i in 1..list.len { |
for i in 1..list.len { |
||
Line 4,566: | Line 4,566: | ||
println('int max: ${max<int>([5,6,4,2,8,3,0,2])}') |
println('int max: ${max<int>([5,6,4,2,8,3,0,2])}') |
||
println('float max: ${max<f64>([1e4, 1e5, 1e2, 1e9])}') |
println('float max: ${max<f64>([1e4, 1e5, 1e2, 1e9])}') |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>int max: 8 |
<pre>int max: 8 |
||
Line 4,573: | Line 4,573: | ||
=={{header|Wart}}== |
=={{header|Wart}}== |
||
Wart defines <code>max</code> in terms of the more general <code>best</code>. |
Wart defines <code>max</code> in terms of the more general <code>best</code>. |
||
< |
<syntaxhighlight lang="python">def (best f seq) |
||
if seq |
if seq |
||
ret winner car.seq |
ret winner car.seq |
||
Line 4,581: | Line 4,581: | ||
def (max ... args) |
def (max ... args) |
||
(best (>) args)</ |
(best (>) args)</syntaxhighlight> |
||
<code>(>)</code> is <code>></code> while suppressing infix expansion. |
<code>(>)</code> is <code>></code> while suppressing infix expansion. |
||
=={{header|WDTE}}== |
=={{header|WDTE}}== |
||
< |
<syntaxhighlight lang="wdte">let s => import 'stream'; |
||
let a => import 'arrays'; |
let a => import 'arrays'; |
||
Line 4,593: | Line 4,593: | ||
-> s.extent 1 > |
-> s.extent 1 > |
||
-> at 0 |
-> at 0 |
||
;</ |
;</syntaxhighlight> |
||
<code>extent</code> is a standard library function that returns a sorted list of the elements of a stream that fit the given function best, so <code>></code> results in the maximum element. |
<code>extent</code> is a standard library function that returns a sorted list of the elements of a stream that fit the given function best, so <code>></code> results in the maximum element. |
||
Line 4,599: | Line 4,599: | ||
=={{header|Wortel}}== |
=={{header|Wortel}}== |
||
The <code>@maxl</code> returns the maximum value of a list: |
The <code>@maxl</code> returns the maximum value of a list: |
||
< |
<syntaxhighlight lang="wortel">@maxl [1 6 4 6 4 8 6 3] ; returns 8</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang="ecmascript">var max = Fn.new { |a| a.reduce { |m, x| (x > m) ? x : m } } |
||
var a = [42, 7, -5, 11.7, 58, 22.31, 59, -18] |
var a = [42, 7, -5, 11.7, 58, 22.31, 59, -18] |
||
System.print(max.call(a))</ |
System.print(max.call(a))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,615: | Line 4,615: | ||
The set of values is the lengths of the lines of text in the input file. |
The set of values is the lengths of the lines of text in the input file. |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \include 'code' declarations |
||
def Tab=$09, LF=$0A, CR=$0D, EOF=$1A; |
def Tab=$09, LF=$0A, CR=$0D, EOF=$1A; |
||
Line 4,642: | Line 4,642: | ||
until C = EOF; |
until C = EOF; |
||
Text(0, "Longest line = "); IntOut(0, Max); CrLf(0); |
Text(0, "Longest line = "); IntOut(0, Max); CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
Example of running the program on its source code: |
Example of running the program on its source code: |
||
Line 4,654: | Line 4,654: | ||
The desired value is the first in a sequence that has been sorted numerically in descending order. |
The desired value is the first in a sequence that has been sorted numerically in descending order. |
||
< |
<syntaxhighlight lang="xml"><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> |
||
<xsl:output method="text"/> |
<xsl:output method="text"/> |
||
Line 4,666: | Line 4,666: | ||
</xsl:for-each> |
</xsl:for-each> |
||
</xsl:template> |
</xsl:template> |
||
</xsl:stylesheet></ |
</xsl:stylesheet></syntaxhighlight> |
||
Sample input: |
Sample input: |
||
< |
<syntaxhighlight lang="xml"><numbers> |
||
<number>3</number> |
<number>3</number> |
||
<number>1</number> |
<number>1</number> |
||
<number>12</number> |
<number>12</number> |
||
<number>7</number> |
<number>7</number> |
||
</numbers></ |
</numbers></syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 4,683: | Line 4,683: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang="yabasic">l$ = "1,1234,62,234,12,34,6" |
||
dim n$(1) |
dim n$(1) |
||
Line 4,695: | Line 4,695: | ||
next |
next |
||
print "Alphabetic order: ", m$, ", numeric order: ", m</ |
print "Alphabetic order: ", m$, ", numeric order: ", m</syntaxhighlight> |
||
=={{header|Yacas}}== |
=={{header|Yacas}}== |
||
Input: |
Input: |
||
< |
<syntaxhighlight lang="yacas">Max({1, 3, 3, 7}) |
||
Max({Pi,Exp(1)+2/5,17*Cos(6)/5,Sqrt(91/10)}) |
Max({Pi,Exp(1)+2/5,17*Cos(6)/5,Sqrt(91/10)}) |
||
Max({1,6,Infinity}) |
Max({1,6,Infinity}) |
||
Max({})</ |
Max({})</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> 7 |
<pre> 7 |
||
Line 4,717: | Line 4,717: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">(1).max(1,2,3) //-->3 |
||
(66).max(1,2,3.14) //-->66</ |
(66).max(1,2,3.14) //-->66</syntaxhighlight> |
||
If given a list, the max of the list is returned. The number/object just selects the method to call. Notice the difference between Int.max and Float.max. |
If given a list, the max of the list is returned. The number/object just selects the method to call. Notice the difference between Int.max and Float.max. |
||
< |
<syntaxhighlight lang="zkl">(66).max(T(1,2,3)) //-->3 |
||
(66).max(T(1,2,3.14)) //-->3 |
(66).max(T(1,2,3.14)) //-->3 |
||
(6.6).max(T(1,2,3.14)) //-->3.14</ |
(6.6).max(T(1,2,3.14)) //-->3.14</syntaxhighlight> |
||
For other object types, you could use: |
For other object types, you could use: |
||
< |
<syntaxhighlight lang="zkl">fcn max{ vm.arglist.reduce(fcn(p,n){ if(p < n) n else p }) }</syntaxhighlight> |
||
<pre>max(2,1,-40,50,2,4,2) //-->50 |
<pre>max(2,1,-40,50,2,4,2) //-->50 |
||
max(2) //-->2 |
max(2) //-->2 |
||
Line 4,732: | Line 4,732: | ||
=={{header|Zoea}}== |
=={{header|Zoea}}== |
||
<syntaxhighlight lang="zoea"> |
|||
<lang Zoea> |
|||
program: max |
program: max |
||
case: 1 |
case: 1 |
||
Line 4,740: | Line 4,740: | ||
input: [1,5,3,2,7] |
input: [1,5,3,2,7] |
||
output: 7 |
output: 7 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
< |
<syntaxhighlight lang="zxbasic">10 PRINT "Values"'' |
||
20 LET z=0 |
20 LET z=0 |
||
30 FOR x=1 TO INT (RND*10)+1 |
30 FOR x=1 TO INT (RND*10)+1 |
||
Line 4,750: | Line 4,750: | ||
60 LET z=(y AND y>z)+(z AND y<z) |
60 LET z=(y AND y>z)+(z AND y<z) |
||
70 NEXT x |
70 NEXT x |
||
80 PRINT '"Max. value = ";z</ |
80 PRINT '"Max. value = ";z</syntaxhighlight> |