Order two numerical lists: Difference between revisions
Content added Content deleted
m (→{{header|Joy}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 17: | Line 17: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">[1,2,1,3,2] < [1,2,0,4,4,0,0,0]</syntaxhighlight> |
||
=={{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 orderlist64.s */ |
/* program orderlist64.s */ |
||
Line 172: | Line 172: | ||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ACL2}}== |
=={{header|ACL2}}== |
||
Line 184: | Line 184: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">INT FUNC Compare(INT ARRAY x INT xLen INT ARRAY y INT yLen) |
||
INT i,len |
INT i,len |
||
Line 247: | Line 247: | ||
Test(c,5,b,6) |
Test(c,5,b,6) |
||
Test(c,5,d,5) |
Test(c,5,d,5) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Order_two_numerical_lists.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Order_two_numerical_lists.png Screenshot from Atari 8-bit computer] |
||
Line 262: | Line 262: | ||
This also includes arrays of user defined types, using the type definition order from smallest to largest. |
This also includes arrays of user defined types, using the type definition order from smallest to largest. |
||
Demonstrated in the program below: |
Demonstrated in the program below: |
||
<syntaxhighlight lang="ada"> |
|||
<lang Ada> |
|||
with Ada.Text_IO; use Ada.Text_IO; |
with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Order is |
procedure Order is |
||
Line 285: | Line 285: | ||
Put_Line (Boolean'Image (List6 > List7)); -- True |
Put_Line (Boolean'Image (List6 > List7)); -- True |
||
end Order; |
end Order; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 296: | Line 296: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">BEGIN # compare lists (rows) of integers # |
||
# returns TRUE if there is an element in a that is < the corresponding # |
# returns TRUE if there is an element in a that is < the corresponding # |
||
# element in b and all previous elements are equal; FALSE otherwise # |
# element in b and all previous elements are equal; FALSE otherwise # |
||
Line 350: | Line 350: | ||
test( "list8", list8, "list7", list7, TRUE ) |
test( "list8", list8, "list7", list7, TRUE ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 364: | Line 364: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
{{Trans|ALGOL 68}} |
{{Trans|ALGOL 68}} |
||
< |
<syntaxhighlight lang="algolw">begin % compare lists (rows) of integers % |
||
% returns TRUE if there is an element in a that is < the corresponding % |
% returns TRUE if there is an element in a that is < the corresponding % |
||
% element in b and all previous elements are equal, FALSE otherwise % |
% element in b and all previous elements are equal, FALSE otherwise % |
||
Line 433: | Line 433: | ||
test( "list7", list7, 1, 3, "list8", list8, 1, 0, false ); |
test( "list7", list7, 1, 3, "list8", list8, 1, 0, false ); |
||
test( "list8", list8, 1, 0, "list7", list7, 1, 3, true ) |
test( "list8", list8, 1, 0, "list7", list7, 1, 3, true ) |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 446: | Line 446: | ||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">ordl(list a, b) |
||
{ |
{ |
||
integer i, l, o; |
integer i, l, o; |
||
Line 473: | Line 473: | ||
0; |
0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
Line 480: | Line 480: | ||
<= is not defined over lists in AppleScript |
<= is not defined over lists in AppleScript |
||
< |
<syntaxhighlight lang="applescript">-- <= for lists |
||
-- compare :: [a] -> [a] -> Bool |
-- compare :: [a] -> [a] -> Bool |
||
on compare(xs, ys) |
on compare(xs, ys) |
||
Line 523: | Line 523: | ||
missing value |
missing value |
||
end if |
end if |
||
end uncons</ |
end uncons</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<lang |
<syntaxhighlight lang="applescript">{false, true}</syntaxhighlight> |
||
---- |
---- |
||
Here's an iterative implementation: |
Here's an iterative implementation: |
||
< |
<syntaxhighlight lang="applescript">on listAComesBeforeListB(a, b) |
||
set aLength to (count a) |
set aLength to (count a) |
||
set bLength to (count b) |
set bLength to (count b) |
||
Line 562: | Line 562: | ||
end repeat |
end repeat |
||
return {a:a, b:b, | a < b |: listAComesBeforeListB(a, b)},</ |
return {a:a, b:b, | a < b |: listAComesBeforeListB(a, b)},</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Examples: |
Examples: |
||
< |
<syntaxhighlight lang="applescript">{a:{3, 5, 6, 5, 2, 1, 4}, b:{5, 8, 9, 2, 0, 1, 9, 0, 4, 10}, | a < b |:true} |
||
{a:{10, 9, 5, 8, 8, 4, 3}, b:{5, 10, 8, 9, 10, 8, 7, 8}, | a < b |:false} |
{a:{10, 9, 5, 8, 8, 4, 3}, b:{5, 10, 8, 9, 10, 8, 7, 8}, | a < b |:false} |
||
{a:{0, 10}, b:{0, 10}, | a < b |:false} |
{a:{0, 10}, b:{0, 10}, | a < b |:false} |
||
{a:{0, 10}, b:{0, 10, 5}, | a < b |:true}</ |
{a:{0, 10}, b:{0, 10, 5}, | a < b |:true}</syntaxhighlight> |
||
=={{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 */ |
||
/* program orderlist.s */ |
/* program orderlist.s */ |
||
Line 728: | Line 728: | ||
pop {r0,r1,r2,r7,lr} @ restaur registers */ |
pop {r0,r1,r2,r7,lr} @ restaur registers */ |
||
bx lr @ return |
bx lr @ return |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">compareLists: function [a,b][ |
||
loop 0..min @[size a, size b] 'i [ |
loop 0..min @[size a, size b] 'i [ |
||
if a\[i] < b\[i] -> return true |
if a\[i] < b\[i] -> return true |
||
Line 744: | Line 744: | ||
do [ |
do [ |
||
print [1 2 1 3 2] <=> [1 2 0 4 4 0 0 0] |
print [1 2 1 3 2] <=> [1 2 0 4 4 0 0 0] |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 753: | Line 753: | ||
{{works with|AutoHotkey_L}} |
{{works with|AutoHotkey_L}} |
||
The function is overkill as we can just compare the list's ObjMaxIndex() |
The function is overkill as we can just compare the list's ObjMaxIndex() |
||
< |
<syntaxhighlight lang="ahk">List1 := [1,2,1,3,2] |
||
List2 := [1,2,0,4,4,0,0,0] |
List2 := [1,2,0,4,4,0,0,0] |
||
MsgBox % order(List1, List2) |
MsgBox % order(List1, List2) |
||
Line 759: | Line 759: | ||
order(L1, L2){ |
order(L1, L2){ |
||
return L1.MaxIndex() < L2.MaxIndex() |
return L1.MaxIndex() < L2.MaxIndex() |
||
}</ |
}</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f ORDER_TWO_NUMERICAL_LISTS.AWK |
# syntax: GAWK -f ORDER_TWO_NUMERICAL_LISTS.AWK |
||
BEGIN { |
BEGIN { |
||
Line 787: | Line 787: | ||
return(ans) |
return(ans) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 798: | Line 798: | ||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
<syntaxhighlight lang="basic256"> |
|||
<lang BASIC256> |
|||
arraybase 1 |
arraybase 1 |
||
dim list1(5): dim list2(6): dim list3(5): dim list4(5) |
dim list1(5): dim list2(6): dim list3(5): dim list4(5) |
||
Line 822: | Line 822: | ||
return l1 < l2 |
return l1 < l2 |
||
end function |
end function |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 831: | Line 831: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
'Ordered before' means 'less than' (see [[Talk:Order_two_numerical_lists|talk page]]). |
'Ordered before' means 'less than' (see [[Talk:Order_two_numerical_lists|talk page]]). |
||
< |
<syntaxhighlight lang="bbcbasic"> DIM list1(4) : list1() = 1, 2, 1, 5, 2 |
||
DIM list2(5) : list2() = 1, 2, 1, 5, 2, 2 |
DIM list2(5) : list2() = 1, 2, 1, 5, 2, 2 |
||
DIM list3(4) : list3() = 1, 2, 3, 4, 5 |
DIM list3(4) : list3() = 1, 2, 3, 4, 5 |
||
Line 849: | Line 849: | ||
IF list1(i%) < list2(i%) THEN = TRUE |
IF list1(i%) < list2(i%) THEN = TRUE |
||
IF list1(i%) > list2(i%) THEN = FALSE |
IF list1(i%) > list2(i%) THEN = FALSE |
||
= l1% < l2%</ |
= l1% < l2%</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 864: | Line 864: | ||
In that case the function outputs FALSE. |
In that case the function outputs FALSE. |
||
Notice that if the arguments are the same, evaluation of the sum produces the product of one of the terms and a factor two. This complicates the pattern a bit. |
Notice that if the arguments are the same, evaluation of the sum produces the product of one of the terms and a factor two. This complicates the pattern a bit. |
||
< |
<syntaxhighlight lang="bracmat">( 1 2 3 4 5:?List1 |
||
& 1 2 1 5 2 2:?List2 |
& 1 2 1 5 2 2:?List2 |
||
& 1 2 1 5 2:?List3 |
& 1 2 1 5 2:?List3 |
||
Line 887: | Line 887: | ||
& gt$(!List5,!List6) |
& gt$(!List5,!List6) |
||
& gt$(!List6,!List7) |
& gt$(!List6,!List7) |
||
);</ |
);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>TRUE |
<pre>TRUE |
||
Line 897: | Line 897: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">int list_cmp(int *a, int la, int *b, int lb) |
||
{ |
{ |
||
int i, l = la; |
int i, l = la; |
||
Line 907: | Line 907: | ||
if (la == lb) return 0; |
if (la == lb) return 0; |
||
return la > lb ? 1 : -1; |
return la > lb ? 1 : -1; |
||
}</ |
}</syntaxhighlight> |
||
This funciton returns one of three states, not a boolean. One can define boolean comparisons, such as <code>list_less_or_eq</code>, based on it:< |
This funciton returns one of three states, not a boolean. One can define boolean comparisons, such as <code>list_less_or_eq</code>, based on it:<syntaxhighlight lang="c">#define list_less_or_eq(a,b,c,d) (list_cmp(a,b,c,d) != 1)</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">namespace RosettaCode.OrderTwoNumericalLists |
||
{ |
{ |
||
using System; |
using System; |
||
Line 953: | Line 953: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>False</pre> |
<pre>False</pre> |
||
Line 959: | Line 959: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
The built-in comparison operators already do this: |
The built-in comparison operators already do this: |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 981: | Line 981: | ||
std::cout << std::boolalpha << (a < b) << std::endl; // prints "false" |
std::cout << std::boolalpha << (a < b) << std::endl; // prints "false" |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|clojure}}== |
=={{header|clojure}}== |
||
< |
<syntaxhighlight lang="clojure"> |
||
(defn lex? [a b] |
(defn lex? [a b] |
||
(compare a b)) |
(compare a b)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun list< (a b) |
||
(cond ((not b) nil) |
(cond ((not b) nil) |
||
((not a) t) |
((not a) t) |
||
((= (first a) (first b)) |
((= (first a) (first b)) |
||
(list< (rest a) (rest b))) |
(list< (rest a) (rest b))) |
||
(t (< (first a) (first b)))))</ |
(t (< (first a) (first b)))))</syntaxhighlight> |
||
Alternate version |
Alternate version |
||
< |
<syntaxhighlight lang="lisp">(defun list< (a b) |
||
(let ((x (find-if-not #'zerop (mapcar #'- a b)))) |
(let ((x (find-if-not #'zerop (mapcar #'- a b)))) |
||
(if x (minusp x) (< (length a) (length b)))))</ |
(if x (minusp x) (< (length a) (length b)))))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
The built-in comparison operators already do this: |
The built-in comparison operators already do this: |
||
< |
<syntaxhighlight lang="d">void main() { |
||
assert([1,2,1,3,2] >= [1,2,0,4,4,0,0,0]); |
assert([1,2,1,3,2] >= [1,2,0,4,4,0,0,0]); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
{{libheader| System.SysUtils}} |
{{libheader| System.SysUtils}} |
||
Line 1,014: | Line 1,014: | ||
{{Trans|Java}} |
{{Trans|Java}} |
||
This is not a full translate of [https://rosettacode.org/wiki/Order_two_numerical_lists#Java Java], just adaptation and aplication of method previus introduced. |
This is not a full translate of [https://rosettacode.org/wiki/Order_two_numerical_lists#Java Java], just adaptation and aplication of method previus introduced. |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Order_two_numerical_lists; |
program Order_two_numerical_lists; |
||
Line 1,049: | Line 1,049: | ||
'not', 'a', 'test'])); |
'not', 'a', 'test'])); |
||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>TRUE |
<pre>TRUE |
||
Line 1,060: | Line 1,060: | ||
=={{header|Ela}}== |
=={{header|Ela}}== |
||
< |
<syntaxhighlight lang="ela">[] <. _ = true |
||
_ <. [] = false |
_ <. [] = false |
||
(x::xs) <. (y::ys) | x == y = xs <. ys |
(x::xs) <. (y::ys) | x == y = xs <. ys |
||
| else = x < y |
| else = x < y |
||
[1,2,1,3,2] <. [1,2,0,4,4,0,0,0]</ |
[1,2,1,3,2] <. [1,2,0,4,4,0,0,0]</syntaxhighlight> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
The built-in comparison functions already do this (not only for lists of numbers, but for any arbitrary data type). |
The built-in comparison functions already do this (not only for lists of numbers, but for any arbitrary data type). |
||
< |
<syntaxhighlight lang="elixir">iex(1)> [1,2,3] < [1,2,3,4] |
||
true |
true |
||
iex(2)> [1,2,3] < [1,2,4] |
iex(2)> [1,2,3] < [1,2,4] |
||
true</ |
true</syntaxhighlight> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
Builtin. Example use from Erlang shell: |
Builtin. Example use from Erlang shell: |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
5> [1,2,3] < [1,2,3,4]. |
5> [1,2,3] < [1,2,3,4]. |
||
true |
true |
||
6> [1,2,3] < [1,2,4]. |
6> [1,2,3] < [1,2,4]. |
||
true |
true |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
By using the Collection.Seq Module the static method Seq.compareWith fits our needs. |
By using the Collection.Seq Module the static method Seq.compareWith fits our needs. |
||
< |
<syntaxhighlight lang="fsharp">let inline cmp x y = if x < y then -1 else if x = y then 0 else 1 |
||
let before (s1 : seq<'a>) (s2 : seq<'a>) = (Seq.compareWith cmp s1 s2) < 0 |
let before (s1 : seq<'a>) (s2 : seq<'a>) = (Seq.compareWith cmp s1 s2) < 0 |
||
Line 1,103: | Line 1,103: | ||
([0; 0], [1]); |
([0; 0], [1]); |
||
] |
] |
||
|> List.iter (fun (x, y) -> printf "%A %s %A\n" x (if before x y then "< " else ">=") y)</ |
|> List.iter (fun (x, y) -> printf "%A %s %A\n" x (if before x y then "< " else ">=") y)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[0] >= [] |
<pre>[0] >= [] |
||
Line 1,127: | Line 1,127: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|BBC BASIC}} |
{{trans|BBC BASIC}} |
||
< |
<syntaxhighlight lang="freebasic">Dim Shared list1(4) As Integer = {1, 2, 1, 5, 2} |
||
Dim Shared list2(5) As Integer = {1, 2, 1, 5, 2, 2} |
Dim Shared list2(5) As Integer = {1, 2, 1, 5, 2, 2} |
||
Dim Shared list3(4) As Integer = {1, 2, 3, 4, 5} |
Dim Shared list3(4) As Integer = {1, 2, 3, 4, 5} |
||
Line 1,148: | Line 1,148: | ||
If Orden(list3(), list4()) Then Print "list3<list4" Else Print "list3>=list4" |
If Orden(list3(), list4()) Then Print "list3<list4" Else Print "list3>=list4" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,156: | Line 1,156: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,228: | Line 1,228: | ||
} |
} |
||
return b |
return b |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,260: | Line 1,260: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution: |
Solution: |
||
< |
<syntaxhighlight lang="groovy">class CList extends ArrayList implements Comparable { |
||
CList() { } |
CList() { } |
||
CList(Collection c) { super(c) } |
CList(Collection c) { super(c) } |
||
Line 1,269: | Line 1,269: | ||
comp ? comp[0] <=> comp[1] : this.size() <=> that.size() |
comp ? comp[0] <=> comp[1] : this.size() <=> that.size() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="groovy">CList a, b; (a, b) = [[], []]; assert ! (a < b) |
||
b = [1] as CList; assert (a < b) |
b = [1] as CList; assert (a < b) |
||
a = [1] as CList; assert ! (a < b) |
a = [1] as CList; assert ! (a < b) |
||
Line 1,280: | Line 1,280: | ||
b = [2, -1, 0] as CList; assert ! (a < b) |
b = [2, -1, 0] as CList; assert ! (a < b) |
||
b = [2, -1, 0, -17] as CList; assert (a < b) |
b = [2, -1, 0, -17] as CList; assert (a < b) |
||
a = [2, 8, 0] as CList; assert ! (a < b)</ |
a = [2, 8, 0] as CList; assert ! (a < b)</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
The built-in comparison operators already do this: |
The built-in comparison operators already do this: |
||
< |
<syntaxhighlight lang="haskell">Prelude> [1,2,1,3,2] < [1,2,0,4,4,0,0,0] |
||
False</ |
False</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
List_llt is written in the style of all Icon/Unicon relational operators returning its right argument if successful and signaling failure otherwise. |
List_llt is written in the style of all Icon/Unicon relational operators returning its right argument if successful and signaling failure otherwise. |
||
< |
<syntaxhighlight lang="icon">procedure main() |
||
write( if list_llt([1,2,1,3,2],[1,2,0,4,4,0,0,0]) then "true" else "false" ) |
write( if list_llt([1,2,1,3,2],[1,2,0,4,4,0,0,0]) then "true" else "false" ) |
||
end |
end |
||
Line 1,300: | Line 1,300: | ||
else if L1[i] >> L2[i] then fail |
else if L1[i] >> L2[i] then fail |
||
if *L1 < *L2 then return L2 |
if *L1 < *L2 then return L2 |
||
end</ |
end</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 1,309: | Line 1,309: | ||
However numeric scalars sort ahead of vectors, i.e. are different from length one lists. |
However numeric scalars sort ahead of vectors, i.e. are different from length one lists. |
||
< |
<syntaxhighlight lang="j">before=: -.@(-: /:~)@,&<~</syntaxhighlight> |
||
< |
<syntaxhighlight lang="j">cmp=: {.@\:@,:&(,&__)</syntaxhighlight> |
||
Below demonstrates non-decreasing order cmp treats length one vector same as scalar |
Below demonstrates non-decreasing order cmp treats length one vector same as scalar |
||
<syntaxhighlight lang="j"> |
|||
<lang j> |
|||
cmp&.>"{~ ('';0;(,0);1;(,1);1 1) |
cmp&.>"{~ ('';0;(,0);1;(,1);1 1) |
||
┌─┬─┬─┬─┬─┬─┐ |
┌─┬─┬─┬─┬─┬─┐ |
||
Line 1,344: | Line 1,344: | ||
├─┼─┼─┼─┼─┼─┤ |
├─┼─┼─┼─┼─┼─┤ |
||
│0│0│0│0│0│0│ |
│0│0│0│0│0│0│ |
||
└─┴─┴─┴─┴─┴─┘</ |
└─┴─┴─┴─┴─┴─┘</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 1,350: | Line 1,350: | ||
{{trans|Common Lisp}} |
{{trans|Common Lisp}} |
||
There are a few methods here. The method named "ordered" which works on arrays is a translation of [[#Common Lisp|Common Lisp]]. The other two are loose translations of [[#Tcl|Tcl]] (some tweaks were needed to get the length checks to work out) and are probably better options. |
There are a few methods here. The method named "ordered" which works on arrays is a translation of [[#Common Lisp|Common Lisp]]. The other two are loose translations of [[#Tcl|Tcl]] (some tweaks were needed to get the length checks to work out) and are probably better options. |
||
< |
<syntaxhighlight lang="java5">import java.util.Arrays; |
||
import java.util.List; |
import java.util.List; |
||
Line 1,383: | Line 1,383: | ||
return i == first.length; |
return i == first.length; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 1,391: | Line 1,391: | ||
<= is already defined for numeric lists in JavaScript |
<= is already defined for numeric lists in JavaScript |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 1,408: | Line 1,408: | ||
// --> [false, true] |
// --> [false, true] |
||
})() |
})() |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<lang |
<syntaxhighlight lang="javascript">[false, true]</syntaxhighlight> |
||
=={{header|Joy}}== |
=={{header|Joy}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="joy"> |
||
DEFINE order == |
DEFINE order == |
||
[equal] [false] |
[equal] [false] |
||
Line 1,432: | Line 1,432: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
jq's builtin comparison operators use lexicographic ordering for arrays in general, not just arrays of integers.< |
jq's builtin comparison operators use lexicographic ordering for arrays in general, not just arrays of integers.<syntaxhighlight lang="jq"> |
||
[1,2,3] < [1,2,3,4] # => true |
[1,2,3] < [1,2,3,4] # => true |
||
[1,2,3] < [1,2,4] # => true |
[1,2,3] < [1,2,4] # => true |
||
[1,2,3] < [1,2,3] # => false</ |
[1,2,3] < [1,2,3] # => false</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang="julia">function islexless(a::AbstractArray{<:Real}, b::AbstractArray{<:Real}) |
||
for (x, y) in zip(a, b) |
for (x, y) in zip(a, b) |
||
if x == y continue end |
if x == y continue end |
||
Line 1,452: | Line 1,452: | ||
println("List not sorted:\n - ", join(tests, "\n - ")) |
println("List not sorted:\n - ", join(tests, "\n - ")) |
||
sort!(tests; lt=islexless) |
sort!(tests; lt=islexless) |
||
println("List sorted:\n - ", join(tests, "\n - "))</ |
println("List sorted:\n - ", join(tests, "\n - "))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,473: | Line 1,473: | ||
=={{header|Klingphix}}== |
=={{header|Klingphix}}== |
||
< |
<syntaxhighlight lang="klingphix">include ..\Utilitys.tlhy |
||
( 1 2 3 ) ( 1 2 3 4 ) less ? |
( 1 2 3 ) ( 1 2 3 4 ) less ? |
||
Line 1,481: | Line 1,481: | ||
( 1 2 3 ) ( 1 2 4 ) less ? |
( 1 2 3 ) ( 1 2 4 ) less ? |
||
"End " input</ |
"End " input</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
operator fun <T> List<T>.compareTo(other: List<T>): Int |
operator fun <T> List<T>.compareTo(other: List<T>): Int |
||
Line 1,511: | Line 1,511: | ||
println() |
println() |
||
for (i in 0 until lists.size - 1) println("list${i + 1} > list${i + 2} = ${lists[i] > lists[i + 1]}") |
for (i in 0 until lists.size - 1) println("list${i + 1} > list${i + 2} = ${lists[i] > lists[i + 1]}") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,537: | Line 1,537: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
This is built into the Lasso comparison operators |
This is built into the Lasso comparison operators |
||
< |
<syntaxhighlight lang="lasso">local( |
||
first = array(1,2,1,3,2), |
first = array(1,2,1,3,2), |
||
second = array(1,2,0,4,4,0,0,0), |
second = array(1,2,0,4,4,0,0,0), |
||
Line 1,547: | Line 1,547: | ||
second = array(1,2,0,4,4,0,0,0), |
second = array(1,2,0,4,4,0,0,0), |
||
) |
) |
||
#first < #second</ |
#first < #second</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>false |
<pre>false |
||
Line 1,555: | Line 1,555: | ||
Uses standard '=' notation |
Uses standard '=' notation |
||
< |
<syntaxhighlight lang="logo">print [1 2] = [1 2] |
||
print [1 2] = [1 2 3] |
print [1 2] = [1 2 3] |
||
print [1 3] = [1 2] |
print [1 3] = [1 2] |
||
Line 1,562: | Line 1,562: | ||
make "list1 [1 2 3 4 5 6] |
make "list1 [1 2 3 4 5 6] |
||
make "list2 [1 2 3 4 5 7] |
make "list2 [1 2 3 4 5 7] |
||
print :list1 = :list2</ |
print :list1 = :list2</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="logo">true |
||
false |
false |
||
false |
false |
||
false |
false |
||
false</ |
false</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 1,575: | Line 1,575: | ||
In Lua tables with numerical indices are used as lists or arrays and they do not support comparison out-of-the-box, so a function is needed to implement the comparison: |
In Lua tables with numerical indices are used as lists or arrays and they do not support comparison out-of-the-box, so a function is needed to implement the comparison: |
||
< |
<syntaxhighlight lang="lua">function arraycompare(a, b) |
||
for i = 1, #a do |
for i = 1, #a do |
||
if b[i] == nil then |
if b[i] == nil then |
||
Line 1,585: | Line 1,585: | ||
end |
end |
||
return true |
return true |
||
end</ |
end</syntaxhighlight> |
||
Here is some demonstration code: |
Here is some demonstration code: |
||
< |
<syntaxhighlight lang="lua">function randomarray() |
||
local t = {} |
local t = {} |
||
for i = 1, math.random(1, 10) do |
for i = 1, math.random(1, 10) do |
||
Line 1,608: | Line 1,608: | ||
arraycompare(a, b) and "<=" or ">", |
arraycompare(a, b) and "<=" or ">", |
||
table.concat(b, ', '))) |
table.concat(b, ', '))) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} (time used as random seed: 1413127434): |
{{out}} (time used as random seed: 1413127434): |
||
Line 1,625: | Line 1,625: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">orderLists := proc(num1,num2) |
||
local len1, len2,i: |
local len1, len2,i: |
||
len1,len2 := numelems(num1),numelems(num2): |
len1,len2 := numelems(num1),numelems(num2): |
||
Line 1,634: | Line 1,634: | ||
end do: |
end do: |
||
return evalb(len1 < len2): |
return evalb(len1 < len2): |
||
end proc:</ |
end proc:</syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">order[List1_, List2_] := With[{ |
||
L1 = List1[[1 ;; Min @@ Length /@ {List1, List2}]], |
L1 = List1[[1 ;; Min @@ Length /@ {List1, List2}]], |
||
L2 = List2[[1 ;; Min @@ Length /@ {List1, List2}]] |
L2 = List2[[1 ;; Min @@ Length /@ {List1, List2}]] |
||
Line 1,644: | Line 1,644: | ||
Length[List1] < Length[List2], |
Length[List1] < Length[List2], |
||
Thread[Order[L1, L2]] == 1 |
Thread[Order[L1, L2]] == 1 |
||
]]</ |
]]</syntaxhighlight> |
||
<pre>Example use: |
<pre>Example use: |
||
Line 1,653: | Line 1,653: | ||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">"<<"(a,b):=block([n:min(length(a),length(b))], |
||
catch(for i thru n do (if a[i]#b[i] then throw(is(a[i]<b[i]))), |
catch(for i thru n do (if a[i]#b[i] then throw(is(a[i]<b[i]))), |
||
throw(is(length(a)<length(b)))))$ |
throw(is(length(a)<length(b)))))$ |
||
Line 1,665: | Line 1,665: | ||
[1,2] << [1,2]; |
[1,2] << [1,2]; |
||
false</ |
false</syntaxhighlight> |
||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
Line 1,671: | Line 1,671: | ||
For a particular numerical type, you can get away with |
For a particular numerical type, you can get away with |
||
< |
<syntaxhighlight lang="mercury">:- pred lt(list(int)::in, list(int)::in) is semidet. |
||
lt([], [_|_]). |
lt([], [_|_]). |
||
lt([H1|T1], [H2|T2]) :- H1 =< H2, T1 `lt` T2.</ |
lt([H1|T1], [H2|T2]) :- H1 =< H2, T1 `lt` T2.</syntaxhighlight> |
||
For a list of any numerical type, one way would be to use a typeclass: |
For a list of any numerical type, one way would be to use a typeclass: |
||
< |
<syntaxhighlight lang="mercury">:- pred lt(list(T)::in, list(T)::in) is semidet <= comparable(T). |
||
lt([], [_|_]). |
lt([], [_|_]). |
||
lt([H1|T1], [H2|T2]) :- H1 =< H2, T1 `lt` T2.</ |
lt([H1|T1], [H2|T2]) :- H1 =< H2, T1 `lt` T2.</syntaxhighlight> |
||
... which you would have to create: |
... which you would have to create: |
||
< |
<syntaxhighlight lang="mercury">:- module comparable. |
||
:- interface. |
:- interface. |
||
:- import_module int, float, integer, list. |
:- import_module int, float, integer, list. |
||
Line 1,709: | Line 1,709: | ||
% pred lt |
% pred lt |
||
% pred lte</ |
% pred lte</syntaxhighlight> |
||
Which would be used in this way - note the typeclass and the comparison operator. |
Which would be used in this way - note the typeclass and the comparison operator. |
||
< |
<syntaxhighlight lang="mercury">:- pred test(list(T), list(T), io, io) <= comparable(T). |
||
:- mode test(in, in, di, uo) is det. |
:- mode test(in, in, di, uo) is det. |
||
test(A, B) --> |
test(A, B) --> |
||
io.write(A), io.write_string(" < "), io.write(B), |
io.write(A), io.write_string(" < "), io.write(B), |
||
io.write_string(" : "), io.write_string(S), io.nl, |
io.write_string(" : "), io.write_string(S), io.nl, |
||
{ A < B -> S = "yes" ; S = "no" }.</ |
{ A < B -> S = "yes" ; S = "no" }.</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">proc `<`[T](a, b: openarray[T]): bool = |
||
for i in 0 .. min(a.len, b.len): |
for i in 0 .. min(a.len, b.len): |
||
if a[i] < b[i]: return true |
if a[i] < b[i]: return true |
||
Line 1,727: | Line 1,727: | ||
return a.len < b.len |
return a.len < b.len |
||
echo([1,2,1,3,2] < [1,2,0,4,4,0,0,0])</ |
echo([1,2,1,3,2] < [1,2,0,4,4,0,0,0])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>false</pre> |
<pre>false</pre> |
||
Line 1,734: | Line 1,734: | ||
The built-in comparison operators already do this for lists (although this is not documented): |
The built-in comparison operators already do this for lists (although this is not documented): |
||
< |
<syntaxhighlight lang="ocaml"># [1;2;1;3;2] < [1;2;0;4;4;0;0;0];; |
||
- : bool = false</ |
- : bool = false</syntaxhighlight> |
||
(Warning: However, the built-in comparison operators do not do this for arrays: |
(Warning: However, the built-in comparison operators do not do this for arrays: |
||
< |
<syntaxhighlight lang="ocaml"># [|1;2;1;3;2|] < [|1;2;0;4;4;0;0;0|];; |
||
- : bool = true</ |
- : bool = true</syntaxhighlight> |
||
) |
) |
||
But we could write it explicitly this way: |
But we could write it explicitly this way: |
||
< |
<syntaxhighlight lang="ocaml">let rec ordered_lists = function |
||
| x1::tl1, x2::tl2 -> |
| x1::tl1, x2::tl2 -> |
||
(match compare x1 x2 with |
(match compare x1 x2 with |
||
Line 1,751: | Line 1,751: | ||
| _ -> true) |
| _ -> true) |
||
| [], _ -> true |
| [], _ -> true |
||
| _ -> false</ |
| _ -> false</syntaxhighlight> |
||
Here is a small script to test this function: |
Here is a small script to test this function: |
||
< |
<syntaxhighlight lang="ocaml">(* copy-paste the code of ordered_lists here *) |
||
let make_num_list p n = |
let make_num_list p n = |
||
Line 1,774: | Line 1,774: | ||
print_num_list lst1; |
print_num_list lst1; |
||
print_num_list lst2; |
print_num_list lst2; |
||
Printf.printf "ordered: %B\n" (ordered_lists (lst1, lst2))</ |
Printf.printf "ordered: %B\n" (ordered_lists (lst1, lst2))</syntaxhighlight> |
||
Sample execution: |
Sample execution: |
||
Line 1,784: | Line 1,784: | ||
Also notice that the function <code>ordered_lists</code> will work with anything the function <code>Pervasives.compare</code> is able to compare (most OCaml types and structures made from the base types). In the prototype of this function below <code>'a list</code> means a list of anything: |
Also notice that the function <code>ordered_lists</code> will work with anything the function <code>Pervasives.compare</code> is able to compare (most OCaml types and structures made from the base types). In the prototype of this function below <code>'a list</code> means a list of anything: |
||
< |
<syntaxhighlight lang="ocaml">val ordered_lists : 'a list * 'a list -> bool</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
Line 1,798: | Line 1,798: | ||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
This sample very similar to Scheme, but implements proper tail recursion. So can test unlimited length lists. |
This sample very similar to Scheme, but implements proper tail recursion. So can test unlimited length lists. |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define (lexorder a b) |
(define (lexorder a b) |
||
(cond |
(cond |
||
Line 1,813: | Line 1,813: | ||
(print (lexorder '(1 2 3) '(1 2 3))) ; => false |
(print (lexorder '(1 2 3) '(1 2 3))) ; => false |
||
(print (lexorder '(1 2 3) '(1 2 8))) ; => true |
(print (lexorder '(1 2 3) '(1 2 8))) ; => true |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
<lang |
<syntaxhighlight lang="parigp">lex(u,v)<1</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
Line 1,856: | Line 1,856: | ||
my $before = orderlists([@$first], [@$second]) ? 'true' : 'false'; |
my $before = orderlists([@$first], [@$second]) ? 'true' : 'false'; |
||
print "(@$first) comes before (@$second) : $before\n"; |
print "(@$first) comes before (@$second) : $before\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,868: | Line 1,868: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Handled natively, eg ("?" is the shorthand print operator) |
Handled natively, eg ("?" is the shorthand print operator) |
||
<!--< |
<!--<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: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}<{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- 1 (true under p2js)</span> |
<span style="color: #0000FF;">?{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}<{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- 1 (true under p2js)</span> |
||
Line 1,875: | Line 1,875: | ||
<span style="color: #0000FF;">?{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}<{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- 0 (false "")</span> |
<span style="color: #0000FF;">?{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}<{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- 0 (false "")</span> |
||
<span style="color: #0000FF;">?{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}<{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- 1 (true "")</span> |
<span style="color: #0000FF;">?{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}<{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- 1 (true "")</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
Elements can be any mix of integers, floating point numbers, strings, or nested subsequences, with atoms ordered before sequences.<br> |
Elements can be any mix of integers, floating point numbers, strings, or nested subsequences, with atoms ordered before sequences.<br> |
||
If you want -1/0/+1 (instead of the true(1)/false(0) shown above), use the builtin compare() function, and that will match under p2js. |
If you want -1/0/+1 (instead of the true(1)/false(0) shown above), use the builtin compare() function, and that will match under p2js. |
||
Line 1,881: | Line 1,881: | ||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
{{trans|Phix}} |
{{trans|Phix}} |
||
< |
<syntaxhighlight lang="phixmonti">include Utilitys.pmt |
||
def ? print nl enddef |
def ? print nl enddef |
||
Line 1,889: | Line 1,889: | ||
( 1 2 4 ) ( 1 2 3 ) < ? |
( 1 2 4 ) ( 1 2 3 ) < ? |
||
( 1 2 3 ) ( 1 2 3 ) < ? |
( 1 2 3 ) ( 1 2 3 ) < ? |
||
( 1 2 3 ) ( 1 2 4 ) < ?</ |
( 1 2 3 ) ( 1 2 4 ) < ?</syntaxhighlight> |
||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
Picat has a built-in general comparison operator <code>@<</code>, which compares terms in lexicographic order (numeric lists as well as atoms, strings, variables, and structures). |
Picat has a built-in general comparison operator <code>@<</code>, which compares terms in lexicographic order (numeric lists as well as atoms, strings, variables, and structures). |
||
< |
<syntaxhighlight lang="picat">go => |
||
Tests = [ |
Tests = [ |
||
[[1,2,3], [1,2,3]], |
[[1,2,3], [1,2,3]], |
||
Line 1,909: | Line 1,909: | ||
printf("%w @< %w: %w\n",L1,L2,cond(L1 @< L2,true,false)) |
printf("%w @< %w: %w\n",L1,L2,cond(L1 @< L2,true,false)) |
||
end, |
end, |
||
nl.</ |
nl.</syntaxhighlight> |
||
In the Picat shell, the result is yes/no rather than true/false: |
In the Picat shell, the result is yes/no rather than true/false: |
||
Line 1,923: | Line 1,923: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
The built-in comparison functions already do this (not only for lists of numbers, but for any arbitrary data type). |
The built-in comparison functions already do this (not only for lists of numbers, but for any arbitrary data type). |
||
< |
<syntaxhighlight lang="picolisp">: (> (1 2 0 4 4 0 0 0) (1 2 1 3 2)) |
||
-> NIL</ |
-> NIL</syntaxhighlight> |
||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang="pike">int(0..1) order_array(array a, array b) |
||
{ |
{ |
||
if (!sizeof(a)) return true; |
if (!sizeof(a)) return true; |
||
Line 1,934: | Line 1,934: | ||
return order_array(a[1..], b[1..]); |
return order_array(a[1..], b[1..]); |
||
return a[0] < b[0]; |
return a[0] < b[0]; |
||
}</ |
}</syntaxhighlight> |
||
Pikes <code>Array.sort_array()</code> function can sort an array of arrays using the <code><</code> operator, but it will sort longer arrays before shorter ones. Therefore the above function is still needed if the intent is to use the comparison for a sort operation. |
Pikes <code>Array.sort_array()</code> function can sort an array of arrays using the <code><</code> operator, but it will sort longer arrays before shorter ones. Therefore the above function is still needed if the intent is to use the comparison for a sort operation. |
||
If the numbers are in 32bit signed integer range, the following works too: |
If the numbers are in 32bit signed integer range, the following works too: |
||
< |
<syntaxhighlight lang="pike">(string)a < (string)b;</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pl/i">lists: procedure options (main); /* 8 June 2014 */ |
||
declare a(10) fixed initial (1, 2, 3, 4, 5, 8, 9, 10, 16, 17), |
declare a(10) fixed initial (1, 2, 3, 4, 5, 8, 9, 10, 16, 17), |
||
Line 1,962: | Line 1,962: | ||
end compare; |
end compare; |
||
end lists;</ |
end lists;</syntaxhighlight> |
||
Results: |
Results: |
||
<pre> |
<pre> |
||
Line 1,971: | Line 1,971: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function order($as,$bs) { |
function order($as,$bs) { |
||
if($as -and $bs) { |
if($as -and $bs) { |
||
Line 1,981: | Line 1,981: | ||
} |
} |
||
"$(order @(1,2,1,3,2) @(1,2,0,4,4,0,0,0))" |
"$(order @(1,2,1,3,2) @(1,2,0,4,4,0,0,0))" |
||
</syntaxhighlight> |
|||
</lang> |
|||
<b>Output:</b> |
<b>Output:</b> |
||
<pre> |
<pre> |
||
Line 1,988: | Line 1,988: | ||
===Non-Recursive Version=== |
===Non-Recursive Version=== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function Test-Order ([int[]]$ReferenceArray, [int[]]$DifferenceArray) |
function Test-Order ([int[]]$ReferenceArray, [int[]]$DifferenceArray) |
||
{ |
{ |
||
Line 2,005: | Line 2,005: | ||
return ($ReferenceArray.Count -lt $DifferenceArray.Count) -or (Compare-Object $ReferenceArray $DifferenceArray) -eq $null |
return ($ReferenceArray.Count -lt $DifferenceArray.Count) -or (Compare-Object $ReferenceArray $DifferenceArray) -eq $null |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
Test-Order -ReferenceArray 1, 2, 1, 3, 2 -DifferenceArray 1, 2, 0, 4, 4, 0, 0, 0 |
Test-Order -ReferenceArray 1, 2, 1, 3, 2 -DifferenceArray 1, 2, 0, 4, 4, 0, 0, 0 |
||
Test-Order -ReferenceArray 1, 2, 1, 3, 2 -DifferenceArray 1, 2, 2, 4, 4, 0, 0, 0 |
Test-Order -ReferenceArray 1, 2, 1, 3, 2 -DifferenceArray 1, 2, 2, 4, 4, 0, 0, 0 |
||
Line 2,012: | Line 2,012: | ||
Test-Order -ReferenceArray 1, 2 -DifferenceArray 1, 2, 3 |
Test-Order -ReferenceArray 1, 2 -DifferenceArray 1, 2, 3 |
||
Test-Order -ReferenceArray 1, 2 -DifferenceArray 1, 2 |
Test-Order -ReferenceArray 1, 2 -DifferenceArray 1, 2 |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 2,023: | Line 2,023: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">DataSection |
||
Array_1: |
Array_1: |
||
Data.i 5 ;element count |
Data.i 5 ;element count |
||
Line 2,093: | Line 2,093: | ||
CloseConsole() |
CloseConsole() |
||
EndIf |
EndIf |
||
</ |
</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>False |
<pre>False |
||
Line 2,103: | Line 2,103: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
The built-in comparison operators already do this: |
The built-in comparison operators already do this: |
||
< |
<syntaxhighlight lang="python">>>> [1,2,1,3,2] < [1,2,0,4,4,0,0,0] |
||
False</ |
False</syntaxhighlight> |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ [ over [] = iff false done |
||
dup [] = iff true done |
dup [] = iff true done |
||
behead rot behead rot |
behead rot behead rot |
||
Line 2,118: | Line 2,118: | ||
' [ [ ] [ 1 ] [ 1 1 1 ] [ 1 2 ] [ 1 2 1 ] [ 2 ] [ 2 1 ] [ 2 1 1 ] ] |
' [ [ ] [ 1 ] [ 1 1 1 ] [ 1 2 ] [ 1 2 1 ] [ 2 ] [ 2 1 ] [ 2 1 1 ] ] |
||
shuffle sortwith []< echo</ |
shuffle sortwith []< echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,126: | Line 2,126: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (lex<? a b) |
(define (lex<? a b) |
||
Line 2,135: | Line 2,135: | ||
(lex<? '(1 2 3 4 5) '(1 2 3 4 4)) ; -> #f |
(lex<? '(1 2 3 4 5) '(1 2 3 4 4)) ; -> #f |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
There is already a built-in comparison operator. |
There is already a built-in comparison operator. |
||
<lang |
<syntaxhighlight lang="raku" line>my @a = <1 2 4>; |
||
my @b = <1 2 4>; |
my @b = <1 2 4>; |
||
say @a," before ",@b," = ", @a before @b; |
say @a," before ",@b," = ", @a before @b; |
||
Line 2,156: | Line 2,156: | ||
my @b = flat @a.map: { Bool.pick ?? $_ !! (^100).roll((0..2).pick) } |
my @b = flat @a.map: { Bool.pick ?? $_ !! (^100).roll((0..2).pick) } |
||
say @a," before ",@b," = ", @a before @b; |
say @a," before ",@b," = ", @a before @b; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 2 4 before 1 2 4 = False |
<pre>1 2 4 before 1 2 4 = False |
||
Line 2,174: | Line 2,174: | ||
=={{header|Rascal}}== |
=={{header|Rascal}}== |
||
The built-in comparison operator already does this: |
The built-in comparison operator already does this: |
||
< |
<syntaxhighlight lang="rascal">rascal>[2,1,3] < [5,2,1,3] |
||
bool: true</ |
bool: true</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 2,181: | Line 2,181: | ||
This example will also work with non-numeric strings. |
This example will also work with non-numeric strings. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program determines if a list < previous list, and returns true or false. */ |
||
@.=; @.1 = 1 2 1 5 2 |
@.=; @.1 = 1 2 1 5 2 |
||
@.2 = 1 2 1 5 2 2 |
@.2 = 1 2 1 5 2 2 |
||
Line 2,204: | Line 2,204: | ||
end /*k*/ |
end /*k*/ |
||
if wx<wy then return 'true' /*handle case of equal (so far). */ |
if wx<wy then return 'true' /*handle case of equal (so far). */ |
||
return 'false' /* " " " " " " */</ |
return 'false' /* " " " " " " */</syntaxhighlight> |
||
{{out|output|:}} |
{{out|output|:}} |
||
<pre> |
<pre> |
||
Line 2,215: | Line 2,215: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
list1 = "1, 2, 1, 5, 2" |
list1 = "1, 2, 1, 5, 2" |
||
list2 = "5, 2, 1, 5, 2, 2" |
list2 = "5, 2, 1, 5, 2, 2" |
||
Line 2,235: | Line 2,235: | ||
func order alist, blist |
func order alist, blist |
||
return strcmp(alist, blist) |
return strcmp(alist, blist) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,245: | Line 2,245: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
The built-in <code><=></code> operator already does this: |
The built-in <code><=></code> operator already does this: |
||
< |
<syntaxhighlight lang="ruby">>> ([1,2,1,3,2] <=> [1,2,0,4,4,0,0,0]) < 0 |
||
=> false</ |
=> false</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
<code>Vec<T></code> implements <code>Ord</code> when <code>T</code> does, so we can just compare them with <code><</code>. (Same with arrays and slices). |
<code>Vec<T></code> implements <code>Ord</code> when <code>T</code> does, so we can just compare them with <code><</code>. (Same with arrays and slices). |
||
< |
<syntaxhighlight lang="rust">vec![1, 2, 1, 3, 2] < vec![1, 2, 0, 4, 4, 0, 0, 0]</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">def lessThan1(a: List[Int], b: List[Int]): Boolean = |
||
if (b.isEmpty) false |
if (b.isEmpty) false |
||
else if (a.isEmpty) true |
else if (a.isEmpty) true |
||
else if (a.head != b.head) a.head < b.head |
else if (a.head != b.head) a.head < b.head |
||
else lessThan1(a.tail, b.tail)</ |
else lessThan1(a.tail, b.tail)</syntaxhighlight><syntaxhighlight lang="scala">def lessThan2(a: List[Int], b: List[Int]): Boolean = (a, b) match { |
||
case (_, Nil) => false |
case (_, Nil) => false |
||
case (Nil, _) => true |
case (Nil, _) => true |
||
case (a :: _, b :: _) if a != b => a < b |
case (a :: _, b :: _) if a != b => a < b |
||
case _ => lessThan2(a.tail, b.tail) |
case _ => lessThan2(a.tail, b.tail) |
||
}</ |
}</syntaxhighlight><syntaxhighlight lang="scala">def lessThan3(a: List[Int], b: List[Int]): Boolean = |
||
a.zipAll(b, Integer.MIN_VALUE, Integer.MIN_VALUE) |
a.zipAll(b, Integer.MIN_VALUE, Integer.MIN_VALUE) |
||
.find{case (a, b) => a != b} |
.find{case (a, b) => a != b} |
||
.map{case (a, b) => a < b} |
.map{case (a, b) => a < b} |
||
.getOrElse(false)</ |
.getOrElse(false)</syntaxhighlight><syntaxhighlight lang="scala">val tests = List( |
||
(List(1, 2, 3), List(1, 2, 3)) -> false, |
(List(1, 2, 3), List(1, 2, 3)) -> false, |
||
(List(3, 2, 1), List(3, 2, 1)) -> false, |
(List(3, 2, 1), List(3, 2, 1)) -> false, |
||
Line 2,279: | Line 2,279: | ||
assert(lessThan2(a, b) == c, test) |
assert(lessThan2(a, b) == c, test) |
||
assert(lessThan3(a, b) == c, test) |
assert(lessThan3(a, b) == c, test) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme">(define (lex<? a b) |
||
(cond ((null? b) #f) |
(cond ((null? b) #f) |
||
((null? a) #t) |
((null? a) #t) |
||
((= (car a) (car b)) (lex<? (cdr a) (cdr b))) |
((= (car a) (car b)) (lex<? (cdr a) (cdr b))) |
||
(else (< (car a) (car b)))))</ |
(else (< (car a) (car b)))))</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
The operator corresponding to the ordering described in this example is less than. |
The operator corresponding to the ordering described in this example is less than. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: main is func |
const proc: main is func |
||
Line 2,303: | Line 2,303: | ||
writeln([] (1) < 0 times 0); # Any nonempty list is not less than the empty list --> FALSE |
writeln([] (1) < 0 times 0); # Any nonempty list is not less than the empty list --> FALSE |
||
writeln(0 times 0 < 0 times 0); # The empty list is not less than the empty list --> FALSE |
writeln(0 times 0 < 0 times 0); # The empty list is not less than the empty list --> FALSE |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,319: | Line 2,319: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
Built-in, via the comparison operator (`<=>`): |
Built-in, via the comparison operator (`<=>`): |
||
< |
<syntaxhighlight lang="ruby">func ordered(a, b) { |
||
(a <=> b) < 0 |
(a <=> b) < 0 |
||
} |
} |
||
Line 2,332: | Line 2,332: | ||
var before = ordered(a, b) |
var before = ordered(a, b) |
||
say "#{a} comes before #{b} : #{before}" |
say "#{a} comes before #{b} : #{before}" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,341: | Line 2,341: | ||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
< |
<syntaxhighlight lang="sml">- List.collate Int.compare ([1,2,1,3,2], [1,2,0,4,4,0,0,0]) = LESS; |
||
val it = false : bool</ |
val it = false : bool</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">let a = [1,2,1,3,2] |
||
let b = [1,2,0,4,4,0,0,0] |
let b = [1,2,0,4,4,0,0,0] |
||
println(lexicographicalCompare(a, b)) // this is "less than"</ |
println(lexicographicalCompare(a, b)) // this is "less than"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,354: | Line 2,354: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc numlist< {A B} { |
||
foreach a $A b $B { |
foreach a $A b $B { |
||
if {$a<$b} { |
if {$a<$b} { |
||
Line 2,363: | Line 2,363: | ||
} |
} |
||
return 0 |
return 0 |
||
}</ |
}</syntaxhighlight> |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
MODE DATA |
MODE DATA |
||
Line 2,391: | Line 2,391: | ||
list1=VALUE(list2) |
list1=VALUE(list2) |
||
ENDLOOP |
ENDLOOP |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre style='height:30ex;overflow:scroll'> |
<pre style='height:30ex;overflow:scroll'> |
||
Line 2,407: | Line 2,407: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang="vb">Private Function order(list1 As Variant, list2 As Variant) As Boolean |
||
i = 1 |
i = 1 |
||
Do While list1(i) <= list2(i) |
Do While list1(i) <= list2(i) |
||
Line 2,426: | Line 2,426: | ||
Debug.Print order([{1, 2, 3, 4}], [{1,2,3}]) |
Debug.Print order([{1, 2, 3, 4}], [{1,2,3}]) |
||
Debug.Print order([{1, 2, 3}], [{1,2,3,4}]) |
Debug.Print order([{1, 2, 3}], [{1,2,3,4}]) |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre>Onwaar |
<pre>Onwaar |
||
Onwaar |
Onwaar |
||
Line 2,432: | Line 2,432: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Function order_list(arr1,arr2) |
Function order_list(arr1,arr2) |
||
order_list = "FAIL" |
order_list = "FAIL" |
||
Line 2,466: | Line 2,466: | ||
WScript.StdOut.WriteLine order_list(Array(0,0),Array(1)) |
WScript.StdOut.WriteLine order_list(Array(0,0),Array(1)) |
||
WScript.StdOut.WriteLine order_list(Array(1,2,1,3,2),Array(1,2,0,4,4,0,0,0)) |
WScript.StdOut.WriteLine order_list(Array(1,2,1,3,2),Array(1,2,0,4,4,0,0,0)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 2,484: | Line 2,484: | ||
=={{header|Wart}}== |
=={{header|Wart}}== |
||
We'll simply overload <code><</code> for lists. |
We'll simply overload <code><</code> for lists. |
||
< |
<syntaxhighlight lang="python">def (a < b) :case (or list?.a list?.b) |
||
if not.b |
if not.b |
||
nil |
nil |
||
Line 2,492: | Line 2,492: | ||
(cdr.a < cdr.b) |
(cdr.a < cdr.b) |
||
:else |
:else |
||
(car.a < car.b)</ |
(car.a < car.b)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,506: | Line 2,506: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang="ecmascript">var orderLists = Fn.new { |l1, l2| |
||
var len = (l1.count <= l2.count) ? l1.count : l2.count |
var len = (l1.count <= l2.count) ? l1.count : l2.count |
||
for (i in 0...len) { |
for (i in 0...len) { |
||
Line 2,531: | Line 2,531: | ||
var res = orderLists.call(lists[i], lists[i+1]) |
var res = orderLists.call(lists[i], lists[i+1]) |
||
System.print("list[%(i)] < list[%(i+1)] -> %(res)") |
System.print("list[%(i)] < list[%(i+1)] -> %(res)") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,556: | Line 2,556: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="yabasic"> |
||
read num : dim list1(4) |
read num : dim list1(4) |
||
read num : dim list2(5) |
read num : dim list2(5) |
||
Line 2,583: | Line 2,583: | ||
data 1, 2, 3, 4, 5 |
data 1, 2, 3, 4, 5 |
||
data 1, 2, 3, 4, 5 |
data 1, 2, 3, 4, 5 |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,591: | Line 2,591: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn listLT(a,b){ |
||
a.walker().zip(b).filter1(fcn([(a,b)]){ a<b }) : // lazy |
a.walker().zip(b).filter1(fcn([(a,b)]){ a<b }) : // lazy |
||
if(_) return(True);; |
if(_) return(True);; |
||
a.len()<b.len() |
a.len()<b.len() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |