Compare a list of strings: Difference between revisions
m
→{{header|Bruijn}}
(correct ordering again) |
|||
(27 intermediate revisions by 23 users not shown) | |||
Line 26:
=={{header|11l}}==
{{trans|D}}
<
V strings = strings_s.split(‘ ’)
print(strings)
print(all(zip(strings, strings[1..]).map(a -> a[0] == a[1])))
print(all(zip(strings, strings[1..]).map(a -> a[0] < a[1])))
print()</
=={{header|360 Assembly}}==
The program uses one ASSIST macro (XPRNT) to keep the code as short as possible.
<
COMPLIST CSECT
USING COMPLIST,R13 base register
Line 122:
PG DS CL80
YREGS
END COMPLIST</
{{out}}
<pre>
Line 128:
AAA : all elements are equal
ACB : neither equal nor in increasing order
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE PTR="CARD"
BYTE FUNC AreEqual(PTR ARRAY a BYTE len)
INT i
FOR i=1 TO len-1
DO
IF SCompare(a(0),a(i))#0 THEN
RETURN (0)
FI
OD
RETURN (1)
BYTE FUNC IsAscendingOrder(PTR ARRAY a BYTE len)
INT i
FOR i=1 TO len-1
DO
IF SCompare(a(i-1),a(i))>=0 THEN
RETURN (0)
FI
OD
RETURN (1)
PROC Test(PTR ARRAY a BYTE len)
INT i
Print("Input array: [")
FOR i=0 TO len-1
DO
Print(a(i))
IF i<len-1 THEN
Put(32)
FI
OD
PrintE("]")
IF AreEqual(a,len) THEN
PrintE("All strings are lexically equal.")
ELSE
PrintE("Not all strings are lexically equal.")
FI
IF IsAscendingOrder(a,len) THEN
PrintE("The list is in strict ascending order.")
ELSE
PrintE("The list is not in strict ascending order.")
FI
PutE()
RETURN
PROC Main()
PTR ARRAY a1(4),a2(4),a3(4),a4(1)
a1(0)="aaa" a1(1)="aaa" a1(2)="aaa" a1(3)="aaa"
Test(a1,4)
a2(0)="aaa" a2(1)="aab" a2(2)="aba" a2(3)="baa"
Test(a2,4)
a3(0)="aaa" a3(1)="aab" a3(2)="aba" a3(3)="aba"
Test(a3,4)
a4(0)="aaa"
Test(a4,1)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Compare_a_list_of_strings.png Screenshot from Atari 8-bit computer]
<pre>
Input array: [aaa aaa aaa aaa]
All strings are lexically equal.
The list is not in strict ascending order.
Input array: [aaa aab aba baa]
Not all strings are lexically equal.
The list is in strict ascending order.
Input array: [aaa aab aba aba]
Not all strings are lexically equal.
The list is not in strict ascending order.
Input array: [aaa]
All strings are lexically equal.
The list is in strict ascending order.
</pre>
Line 133 ⟶ 220:
We will store the "list" of strings in a vector. The vector will hold "indefinite" strings, i.e., the strings can have different lengths.
<
(Index_Type => Positive, Element_Type => String);
use type String_Vec.Vector;</
The equality test iterates from the first to the last-but one index. For index Idx,
Line 142 ⟶ 229:
yes for any Idx, the function immediately returns False. If the answer is no for all Idx,
the function finally returns True.
<
begin
for Idx in Strings.First_Index .. Strings.Last_Index-1 loop
Line 150 ⟶ 237:
end loop;
return True;
end All_Are_The_Same;</
Similarily, the strictly ascending test checks if Strings(Idx) is greater or equal Strings(Idx+1).
<
begin
for Idx in Strings.First_Index+1 .. Strings.Last_Index loop
Line 161 ⟶ 248:
end loop;
return True;
end Strictly_Ascending;</
If the variable Strings is of the type String_Vec.vector, one can call these two functions
as usual.
<
Boolean'Image(Strictly_Ascending(Strings)));</
If Strings holds two or more strings, the result will be either of TRUE, FALSE, or FALSE, TRUE, or FALSE, FALSE, indicating all strings are the same, or they are strictly ascending, or neither.
Line 172 ⟶ 259:
=={{header|ALGOL 68}}==
<
[]STRING list2 = ("AA","AA","AA");
[]STRING list3 = ("AA","CC","BB");
Line 184 ⟶ 271:
BOOL ok := TRUE;
FOR i TO UPB list - 1 WHILE ok DO
OD;
ok
Line 193 ⟶ 280:
BOOL ok := TRUE;
FOR i TO UPB list - 1 WHILE ok DO
OD;
ok
Line 211 ⟶ 298:
print (("...is not in strict ascending order", new line))
FI
OD</
{{out}}
<pre>list: AA BB CC
Line 231 ⟶ 318:
=={{header|ALGOL W}}==
<
% As Algol W procedures cannot determine the bounds of an array, the bounds %
% must be specified in lo and hi %
Line 266 ⟶ 353:
end;
ordered
end ascendingOrder ;</
=={{header|AppleScript}}==
Line 274 ⟶ 361:
<
on allEqual(xs)
_and(zipWith(my _equal, xs, rest of xs))
Line 382 ⟶ 469:
end script
end if
end mReturn</
{{Out}}
Line 389 ⟶ 476:
=={{header|Arturo}}==
<
ascending?: function [lst] -> lst = sort lst
Line 403 ⟶ 490:
print ["allEqual?" allEqual? l]
print ["ascending?" ascending? l "\n"]
]</
{{out}}
Line 424 ⟶ 511:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f COMPARE_A_LIST_OF_STRINGS.AWK
BEGIN {
Line 452 ⟶ 539:
printf("\n%d\n%d\n",test1,test2)
}
</syntaxhighlight>
{{out}}
<pre>
Line 475 ⟶ 562:
1
</pre>
=={{header|BQN}}==
Line 481 ⟶ 567:
If all are not equal and the list is invariant under sorting, then it is in ascending order.
<
Asc ← ¬∘AllEq∧∧≡⊢
Line 488 ⟶ 574:
•Show AllEq ⟨"AA", "ACB", "BB", "CC"⟩
•Show Asc ⟨"AA", "ACB", "BB", "CC"⟩</
<
0
0
1</
[https://mlochbaum.github.io/BQN/try.html#code=QWxsRXEg4oaQIOKNi+KJoeKNkgpBc2Mg4oaQIMKs4oiYQWxsRXHiiKfiiKfiiaHiiqIKCuKAolNob3cgQWxsRXEg4p+oIkFBIiwgIkFBIiwgIkFBIiwgIkFBIuKfqQrigKJTaG93IEFzYyDin6giQUEiLCAiQUEiLCAiQUEiLCAiQUEi4p+pCgrigKJTaG93IEFsbEVxIOKfqCJBQSIsICJBQ0IiLCAiQkIiLCAiQ0Mi4p+pCuKAolNob3cgQXNjIOKfqCJBQSIsICJBQ0IiLCAiQkIiLCAiQ0Mi4p+pCg== Try It!]
Line 524 ⟶ 610:
<
& (test2=x.~(!arg:? %@?x (%@:~>!x) ?))</
Demonstration
<
. isiZulu
isiXhosa
Line 583 ⟶ 669:
)
)
</syntaxhighlight>
'''Output'''
<pre>test1 lstA fails
Line 592 ⟶ 678:
test2 lstC fails</pre>
=={{header|Bruijn}}==
{{trans|Haskell}}
<syntaxhighlight lang="bruijn">
:import std/String .
all-eq? [land? (zip-with eq? 0 (tail 0))]
all-gt? [land? (zip-with lt? 0 (tail 0))]
# --- tests ---
list-a "abc" : ("abc" : {}("abc"))
list-b "abc" : ("def" : {}("ghi"))
:test (all-eq? list-a) ([[1]])
:test (all-eq? list-b) ([[0]])
:test (all-gt? list-a) ([[0]])
:test (all-gt? list-b) ([[1]])
</syntaxhighlight>
=={{header|C}}==
<
#include <string.h>
Line 613 ⟶ 720:
return false;
return true;
}</
=={{header|C sharp|C#}}==
{{works with|C sharp|7}}
<
strings.Count < 2 ? (true, true) :
(
strings.Distinct().Count() < 2,
Enumerable.Range(1, strings.Count - 1).All(i => string.Compare(strings[i-1], strings[i]) < 0)
);</
=={{header|C++}}==
Line 629 ⟶ 736:
{{works with|C++|11}}
<
#include <string>
Line 637 ⟶ 744:
std::is_sorted( strings.begin(), strings.end(),
[](std::string a, std::string b){ return !(b < a); }) ) // Strictly ascending</
=={{header|Clojure}}==
Used similar approach as the Python solution
<
;; Checks if all items in strings list are equal (returns true if list is empty)
Line 650 ⟶ 757:
(every? (fn [[a nexta]] (<= (compare a nexta) 0)) (map vector strings (rest strings))))
</syntaxhighlight>
=={{header|COBOL}}==
{{works with|GnuCOBOL}}
<
program-id. CompareLists.
Line 713 ⟶ 820:
end-if
display " "
.</
{{out}}
<pre>list:
Line 742 ⟶ 849:
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
(defun strings-equal-p (strings)
(null (remove (first strings) (rest strings) :test #'string=)))
Line 750 ⟶ 857:
for string2 in (rest strings)
always (string-lessp string1 string2)))
</syntaxhighlight>
=={{header|D}}==
<
import std.stdio, std.algorithm, std.range, std.string;
Line 762 ⟶ 869:
writeln;
}
}</
{{out}}
<pre>["AA", "AA", "AA", "AA"]
Line 775 ⟶ 882:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Compare_a_list_of_strings;
Line 843 ⟶ 950:
readln;
end.</
{{out}}
<pre>[a]
Line 859 ⟶ 966:
=={{header|Dyalect}}==
<
var prev
for x in xs {
Line 879 ⟶ 986:
}
true
}</
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight lang=easylang>
proc test s$[] . .
ident = 1
ascend = 1
for i = 2 to len s$[]
h = strcmp s$[i] s$[i - 1]
if h <> 0
ident = 0
.
if h <= 0
ascend = 0
.
.
print s$[]
if ident = 1
print "all equal"
.
if ascend = 1
print "ascending"
.
print ""
.
test [ "AA" "BB" "CC" ]
test [ "AA" "AA" "AA" ]
test [ "AA" "CC" "BB" ]
test [ "AA" "ACB" "BB" "CC" ]
test [ "single_element" ]
</syntaxhighlight>
=={{header|Elena}}==
ELENA
<
import system'routines;
import extensions;
Line 890 ⟶ 1,029:
{
isEqual()
= nil == self.seekEach(self.FirstMember, (n,m => m
isAscending()
Line 899 ⟶ 1,038:
later.next();
^ nil == former.zipBy(later, (prev,next => next <= prev )).seekEach::(b => b)
}
}
Line 913 ⟶ 1,052:
public program()
{
testCases.forEach::(list)
{
console.printLine(list.asEnumerable()," all equal - ",list.isEqual());
Line 920 ⟶ 1,059:
console.readChar()
}</
{{out}}
<pre>
Line 926 ⟶ 1,065:
AA,BB,CC ascending - true
AA,AA,AA all equal - true
AA,AA,AA ascending -
AA,CC,BB all equal - false
AA,CC,BB ascending - false
Line 936 ⟶ 1,075:
=={{header|Elixir}}==
<
def compare_strings(strings) do
{length(Enum.uniq(strings))<=1, strict_ascending(strings)}
Line 949 ⟶ 1,088:
Enum.each(lists, fn list ->
IO.puts "#{inspect RC.compare_strings(list)}\t<= #{inspect list} "
end)</
{{out}}
Line 963 ⟶ 1,102:
{{trans|Haskell}}
<
-module(compare_strings).
Line 976 ⟶ 1,115:
all_fulfill(Fun,Strings) ->
lists:all(fun(X) -> X end,lists:zipwith(Fun, lists:droplast(Strings), tl(Strings)) ).
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
<
let ascending strings = Seq.isEmpty strings || Seq.forall2 (fun x y -> x < y) strings (Seq.tail strings)</
<p>Actually <code>allEqual</code> is a shortcut and <code>ascending</code> is a general pattern. We can make a function
out of it which constructs a new function from a comparision function</p>
<
<p>and define the 2 task functions that way</p>
<
let ascending = !(<)</
<p>getting something similar as the builtin in Raku</p>
=={{header|Factor}}==
Assuming the list is on top of the data stack, testing for lexical equality:
<
all-equal?</
Testing for ascending order:
<
[ before? ] monotonic?</
=={{header|Forth}}==
Line 1,003 ⟶ 1,142:
Raw Forth is a very low level language and has no Native lists so we have to build from scratch.
Remarkably by concatenating these low level operations and using the simple Forth parser we can build the linked lists of strings and the list operators quite simply. The operators and lists that we create become extensions to the language.
<
: ," ( -- ) [CHAR] " WORD c@ 1+ ALLOT ; \ Parse input stream until " and write into next available memory
: [[ ( -- ) 0 C, ; \ begin a list. write a 0 into next memory byte (null string)
Line 1,046 ⟶ 1,185:
create strings [[ ," ENTRY 4" ," ENTRY 3" ," ENTRY 2" ," ENTRY 1" ]]
create strings2 [[ ," the same" ," the same" ," the same" ]]
create strings3 [[ ," AAA" ," BBB" ," CCC" ," DDD" ]] </
Test at the Forth console
Line 1,064 ⟶ 1,203:
Application code is hard to write and hard to read when low-level code is mixed in with application code.
It is better to hide low-level code in general-purpose code-libraries so that the application code can be simple.
Here is my solution using LIST.4TH from my novice-package:
<
: test-equality ( string node -- new-string bad? )
over count \ -- string node adr cnt
Line 1,079 ⟶ 1,218:
seq .line @ seq 2nd 'test find-node
nip 0= ;
</syntaxhighlight>
Here is a test of the above code:
{{out}}
Line 1,103 ⟶ 1,242:
On the other hand a function such as ALLINORDER would show the sound of one hand clapping. It would also reveal the order in which comparisons were made, and whether the loop would quit on the first failure or blockheadedly slog on through the lot regardless. Alas, on these questions the documentation for ALL is suspiciously silent.
<syntaxhighlight lang="fortran">
INTEGER MANY,LONG
PARAMETER (LONG = 6,MANY = 4) !Adjust to suit.
Line 1,122 ⟶ 1,261:
END IF
END
</
And yes, if MANY is set to one and the extra texts are commented out, the results are both true, and ungrammatical statements are made. Honest. Possibly, another special function, as in <code>COUNT(STRINGS(1:MANY - 1) .LT. STRINGS(2:MANY)))</code> would involve less one-hand-clapping when there are no comparisons to make, but the production of a report that would use it is not in the specification.
Line 1,128 ⟶ 1,267:
===F2003-F2008===
F2008 standard ([ISO 2010], 4.4.3) defines the character variable of the character type as a set of values composed of character strings and a character string is a sequence of characters, numbered from left to right 1, 2, 3, ... up to the number of characters in the string. The number of characters in the string is called the length of the string. The length is a type parameter; its kind is processor dependent and its value is greater than or equal to zero. I.e in declaration
<syntaxhighlight lang="fortran">
character (len=12) :: surname
</syntaxhighlight>
keyword len is NOT a size of array, it is an intrinsic parameter of character type, and character type is in fortran a [[first-class type]]: they can be assigned as objects or passed as parameters to a subroutine.
In summary, the character data type in Fortran is a real, first class data type. Fortran character strings are not hacked-up arrays!
<syntaxhighlight lang="fortran">
program compare_char_list
implicit none
Line 1,152 ⟶ 1,291:
end if
end program compare_char_list
</syntaxhighlight>
=={{header|FreeBASIC}}==
<
' FB 1.05.0 Win64
Line 1,175 ⟶ 1,314:
Return True
End Function
</syntaxhighlight>
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Compare_a_list_of_strings}}
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn ListObjectsAreIdentical( array as CFArrayRef ) as BOOL
BOOL result = NO
CFSetRef set = fn SetWithArray( array )
result = ( fn SetCount( set ) <= 1 )
end fn = result
local fn ListIsInLexicalOrder( array as CFArrayRef ) as BOOL
BOOL result = NO
CFArrayRef sortedArray = fn ArraySortedArrayUsingSelector( array, @"compare:" )
result = fn ArrayIsEqual( array, sortedArray )
end fn = result
void local fn ListTest
long i
CFArrayRef listA = @[@"aaa", @"aaa", @"aaa", @"aaa"]
CFArrayRef listB = @[@"aaa", @"aab", @"aba", @"baa"]
CFArrayRef listC = @[@"caa", @"aab", @"aca", @"abc"]
CFArrayRef lists = @[listA, listB, listC]
for i = 0 to 2
CFArrayRef temp = lists[i]
printf @"Input array elements: %@ %@ %@ %@", temp[0], temp[1], temp[2], temp[3]
if ( fn ListObjectsAreIdentical( temp ) )
printf @"List elements are lexically equal."
else
printf @"List elements not lexically equal."
end if
if ( fn ListIsInLexicalOrder( temp ) == YES )
printf @"List elements are in ascending order."
else
printf @"List elements not in ascending order."
end if
CFArrayRef sorted = fn ArraySortedArrayUsingSelector( temp, @"compare:" )
printf @"List elements sorted in ascending order: %@ %@ %@ %@", sorted[0], sorted[1], sorted[2], sorted[3]
print
next
end fn
fn ListTest
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Input array elements: aaa aaa aaa aaa
List elements are lexically equal.
List elements are in ascending order.
List elements sorted in ascending order: aaa aaa aaa aaa
Input array elements: aaa aab aba baa
List elements not lexically equal.
List elements are in ascending order.
List elements sorted in ascending order: aaa aab aba baa
Input array elements: caa aab aca abc
List elements not lexically equal.
List elements not in ascending order.
List elements sorted in ascending order: aab abc aca caa
</pre>
=={{header|Go}}==
<
func AllEqual(strings []string) bool {
Line 1,204 ⟶ 1,406:
}
return true
}</
See [[Compare_a_list_of_strings/GoTests]] for validation tests.
Line 1,210 ⟶ 1,412:
=={{header|Gosu}}==
<
var isHomogeneous = list.toSet().Count < 2
var isOrderedSet = list.toSet().order().toList() == list</
=={{header|Haskell}}==
<
allEqual xs = and $ zipWith (==) xs (tail xs)
allIncr :: Ord a => [a] -> Bool
allIncr xs = and $ zipWith (<) xs (tail xs)</
Alternatively, using folds:
<
:: Eq a
=> [a] -> Bool
Line 1,235 ⟶ 1,437:
=> [a] -> Bool
allIncreasing [] = True
allIncreasing (h:t) = fst $ foldl (\(a, x) y -> (a && x < y, y)) (True, h) t</
or seeking earlier exit (from longer lists) with '''until''', but in fact, perhaps due to lazy execution, the zipWith at the top performs best.
<
:: Eq a
=> [a] -> Bool
Line 1,259 ⟶ 1,461:
(\(x, xs) -> null xs || x >= head xs)
(\(_, x:xs) -> (x, xs))
(h, t)</
=={{header|Icon}} and {{header|Unicon}}==
Icon and Unicon expressions either succeed and return a value (which may be &null) or fail.
<
# list-compare.icn
#
Line 1,305 ⟶ 1,507:
}
return
end</
{{out}}
Line 1,317 ⟶ 1,519:
=={{header|J}}==
'''Solution''' (''equality test''):<
'''Solution''' (''order test''):<
'''Notes''': <tt>asc</tt> indicates whether <tt>y</tt> is monotonically increasing, but not necessarily strictly monotonically increasing (in other words, it allows equal elements if they are adjacent to each other).
=={{header|Java}}==
This is a fairly basic procedure in Java, using <kbd>for-loop</kbd>s, <code>String.equals</code>, and <code>String.compareTo</code>.
<syntaxhighlight lang="java">
boolean allEqual(String[] strings) {
String stringA = strings[0];
for (String string : strings) {
if (!string.equals(stringA))
return false;
}
return true;
}
</syntaxhighlight>
<syntaxhighlight lang="java">
boolean isAscending(String[] strings) {
String previous = strings[0];
int index = 0;
for (String string : strings) {
if (index++ == 0)
continue;
if (string.compareTo(previous) < 0)
return false;
previous = string;
}
return true;
}
</syntaxhighlight>
<br />
Alternately,
{{works with|Java|8}}
<
public class CompareListOfStrings {
Line 1,335 ⟶ 1,564:
}
}
}</
{{out}}
<pre>[AA, AA, AA, AA]
Line 1,347 ⟶ 1,576:
===ES5===
====Iterative====
<
var out = true, i = 0;
while (++i<a.length) {
Line 1,370 ⟶ 1,599:
console.log(azSorted(empty)); // true
console.log(azSorted(single)); // true
</syntaxhighlight>
===ES6===
Line 1,376 ⟶ 1,605:
Using a generic zipWith, and functionally composed predicates:
<
'use strict';
Line 1,426 ⟶ 1,655:
};
})();</
{{Out}}
<syntaxhighlight lang="javascript">{
"allEqual": [
false,
Line 1,440 ⟶ 1,669:
true
]
}</
=={{header|jq}}==
Line 1,447 ⟶ 1,676:
For both the following functions, the input is assumed to be a (possibly empty) array of strings.
In both cases also, the implementations are fast but could be improved at the expense of complexity.
<
def lexically_equal:
. as $in
Line 1,457 ⟶ 1,686:
. as $in
| reduce range(0;length-1) as $i
(true; if . then $in[$i] < $in[$i + 1] else false end);</
'''Examples''':
<
<
=={{header|Jsish}}==
Code from Javascript, ES5.
<
function allEqual(a) {
var out = true, i = 0;
Line 1,484 ⟶ 1,713:
if (allAscending(strings)) puts("strings array in strict ascending order");
else puts("strings array not in strict ascending order");</
{{out}}
Line 1,492 ⟶ 1,721:
{{works with|Julia|0.6}}
<
test = [["RC", "RC", "RC"], ["RC", "RC", "Rc"], ["RA", "RB", "RC"],
Line 1,501 ⟶ 1,730:
println("The elements are $("not " ^ !allequal(v))all equal.")
println("The elements are $("not " ^ !issorted(v))strictly increasing.")
end</
{{out}}
Line 1,533 ⟶ 1,762:
=={{header|Klong}}==
<syntaxhighlight lang="k">
{:[2>#x;1;&/=:'x]}:(["test" "test" "test"])
1
{:[2>#x;1;&/<:'x]}:(["bar" "baz" "foo"])
1
</syntaxhighlight>
=={{header|Kotlin}}==
<
fun areEqual(strings: Array<String>): Boolean {
Line 1,560 ⟶ 1,789:
else if (areAscending(args)) println("They are in strictly ascending order")
else println("They are neither equal nor in ascending order")
}</
Sample input/output:
{{out}}
Line 1,569 ⟶ 1,798:
=={{header|Lambdatalk}}==
<
{def allsame
{def allsame.r
Line 1,612 ⟶ 1,841:
} -> true false false true true
</syntaxhighlight>
=={{header|Lua}}==
<
_, fst = next(t_str)
if fst then
Line 1,654 ⟶ 1,883:
check("AA,CC,BB")
check("AA,ACB,BB,CC")
check("single_element")</
{{out}}
<pre>ayu dab dog gar panda tui yak: not identical and ascending.
Line 1,667 ⟶ 1,896:
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Function Equal(Strings){
Line 1,707 ⟶ 1,936:
}
Checkit
</syntaxhighlight>
=={{header|Maple}}==
<
local i:
for i from 2 to numelems(lst) do
Line 1,726 ⟶ 1,955:
tst := ["abc","abc","abc","abc","abc"]:
lexEqual(tst):
lexAscending(tst):</
{{Out|Examples}}
<pre>true
false</pre>
=={{header|Mathcad}}==
Mathcad is a non-text-based programming environment. The expressions below are an approximations of the way that they are entered (and) displayed on a Mathcad worksheet. The worksheet is available at xxx_tbd_xxx
This particular version of "Compare a list of strings" was created in Mathcad Prime Express 7.0, a free version of Mathcad Prime 7.0 with restrictions (such as no programming or symbolics). All Mathcad numbers are complex doubles. There is a recursion depth limit of about 4,500. Strings are a distinct data and are not conceptually a list of integers.
<syntaxhighlight lang="mathcad">-- define list of list of strings (nested vector of vectors of strings)
-- Mathcad vectors are single column arrays.
-- The following notation is for convenience in writing arrays in text form.
-- Mathcad array input is normally via Mathcad's array operator or via one of the
-- array-builder functions, such as stack or augment.
-- "," between vector elements indicates a new row.
-- " " between vector elements indicates a new column.
list:=["AA","AA","AA"],["AA","BB","CC"],["AA","CC","BB"],["CC","BB","AA"],["AA","ACB","BB","CC"],["AA"]]
-- define functions head and rest that return the first value in a list (vector)
-- and the list excluding the first element, respectively.
head(v):=if(IsArray(v),v[0,v)
rest(v):=if(rows(v)>1,submatrix(v,1,rows(v)-1,0,0),0)
-- define a function oprel that iterates through a list (vector) applying a comparison operator op to each pair of elements at the top of the list.
-- Returns immediately with false (0) if a comparison fails.
oprel(op,lst,val):=if(op(val,head(lst)),if(rows(lst)>1,oprel(op,rest(lst),head(lst)),1),0)
oprel(op,lst):=if(rows(lst)>1,oprel(op,rest(lst),head(lst)),1)
-- define a set of boolean comparison functions
-- transpose represents Mathcad's transpose operator
-- vectorize represents Mathcad's vectorize operator
eq(a,b):=a=b (transpose(vectorize(oprel,list))) = [1 0 0 0 0 1] -- equal
lt(a,b):=a<b (transpose(vectorize(oprel,list))) = [0 1 0 0 1 1] -- ascending
-- oprel, eq and lt also work with numeric values
list:=[11,11,11],[11,22,33],[11,33,22],[33,22,11],[11,132,22,33],[11]]
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Apply[Equal, data]
OrderedQ[data]</
{{out}}
<pre>False
Line 1,741 ⟶ 2,010:
=={{header|MATLAB}} / {{header|Octave}}==
Only the first task is implemented.
<
all(strcmp(alist,alist{1}))</
=={{header|Nanoquery}}==
<
def stringsEqual(stringList)
// if the list is empty, return true
Line 1,781 ⟶ 2,050:
// return whether the string were less than each other or not
return lessThan
end</
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols nobinary
Line 1,830 ⟶ 2,099:
return
</syntaxhighlight>
{{out}}
<pre>
Line 1,851 ⟶ 2,120:
This is the obvious (and more efficient way) to compare strings in Nim:
<syntaxhighlight lang="nim">
func allEqual(s: openArray[string]): bool =
Line 1,873 ⟶ 2,142:
doAssert allEqual(["abc"])
doAssert ascending(["abc"])</
For “allEqual”, there is another simple way using template “allIt” from standard module “sequtils”:
<
func allEqual(s: openArray[string]): bool =
Line 1,884 ⟶ 2,153:
doAssert allEqual(["abc", "abc", "abc"])
doAssert not allEqual(["abc", "abd", "abc"])
doAssert allEqual(["abc"])</
There are other less obvious and less efficient ways, using hash sets, sorting or “map” and “zip”.
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">
open List;;
Line 1,919 ⟶ 2,188:
List.iter test [lasc;leq;lnoasc];;
</syntaxhighlight>
{{out}}
<pre>
Line 1,944 ⟶ 2,213:
=={{header|Oforth}}==
<
: lexCmp(l) l l right( l size 1- ) zipWith(#<) and ;</
=={{header|ooRexx}}==
<
* 28.06.2014 Walter Pachl
*--------------------------------------------------------------------*/
Line 1,979 ⟶ 2,248:
Say 'List' name': neither equal nor in increasing order'
End
Return</
{{out}}
<pre>List ABC: elements are in increasing order
Line 1,987 ⟶ 2,256:
=={{header|PARI/GP}}==
Easiest is to use <code>Set()</code>:
<
inOrder(strings)=Set(strings)==strings</
More efficient:
<
inOrder(strings)=for(i=2,#strings,if(strings[i]>strings[i-1], return(0))); 1</
=={{header|Perl}}==
<
all { $strings[0] eq $strings[$_] } 1..$#strings # All equal
all { $strings[$_-1] lt $strings[$_] } 1..$#strings # Strictly ascending</
Alternatively, if you can guarantee that the input strings don't contain null bytes, the equality test can be performed by a regex like this:
<
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">allsame</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">strict_order</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]<=</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%-22V allsame:%5t, strict_order:%5t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">allsame</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #000000;">strict_order</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"AA"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"BB"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"CC"</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"AA"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"AA"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"AA"</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"AA"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"CC"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"BB"</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"AA"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ACB"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"BB"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"CC"</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"single_element"</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
</pre>
=={{header|Phixmonti}}==
<
( "alpha" "beta" "gamma" "delta" "epsilon" "zeta"
Line 2,049 ⟶ 2,321:
dup len swap 1 get rot repeat == /# put 0 (false) in the pile, indicating that they are not repeated strings #/
</syntaxhighlight>
=={{header|Picat}}==
<syntaxhighlight lang="picat">main =>
Lists = [["AA","BB","CC"],
["AA","AA","AA"],
["AA","CC","BB"],
["AA","ACB","BB","CC"],
["single_element"],
[]],
foreach(L in Lists)
Same = all_same(L).cond(true,false),
Sorted = sorted(L).cond(true,false),
printf("%-18w all_same:%-5w sorted:%-5w\n",L,Same,Sorted)
end.
all_same([]).
all_same([_]).
all_same([A,B|Rest]) :-
A == B,
all_same([B|Rest]).</syntaxhighlight>
{{out}}
<pre>[AA,BB,CC] all_same:false sorted:true
[AA,AA,AA] all_same:true sorted:true
[AA,CC,BB] all_same:false sorted:false
[AA,ACB,BB,CC] all_same:false sorted:true
[single_element] all_same:true sorted:true
[] all_same:true sorted:true </pre>
=={{header|PicoLisp}}==
PicoLisp has the native operators =, > and < these can take an infinite number of arguments and are also able to compare Transient symbols (the Strings of PicoLisp).
<
-> T
(= "AA" "AA" "Aa")
Line 2,064 ⟶ 2,364:
-> T
(> "A" "B" "Z" "C")
-> NIL</
If you want a function which takes one list here are some straight-forward implementation:
<syntaxhighlight lang="picolisp">
(de same (List)
(apply = List))
Line 2,078 ⟶ 2,378:
(same '("AA" "AA" "AA"))
-> T
</syntaxhighlight>
This would of course also work with <= and >= without any hassle.
=={{header|PL/I}}==
<
/*--------------------------------------------------------------------
* 01.07.2014 Walter Pachl
Line 2,121 ⟶ 2,421:
Put Skip List(name!!': '!!txt);
End;
End;</
{{out}}
<pre>ABC: elements are in increasing order
Line 2,128 ⟶ 2,428:
=={{header|Plain English}}==
<
If the string things are empty, say yes.
Get a string thing from the string things.
Line 2,146 ⟶ 2,446:
If the string thing's string is less than the string thing's previous' string, say no.
Put the string thing's next into the string thing.
Repeat.</
=={{header|PowerShell}}==
{{works with|PowerShell|4.0}}
<syntaxhighlight lang="powershell">
function IsAscending ( [string[]]$Array ) { ( 0..( $Array.Count - 2 ) ).Where{ $Array[$_] -le $Array[$_+1] }.Count -eq $Array.Count - 1 }
function IsEqual ( [string[]]$Array ) { ( 0..( $Array.Count - 2 ) ).Where{ $Array[$_] -eq $Array[$_+1] }.Count -eq $Array.Count - 1 }
Line 2,161 ⟶ 2,461:
IsEqual 'A', 'C', 'B', 'C'
IsEqual 'A', 'A', 'A', 'A'
</syntaxhighlight>
{{out}}
<pre>
Line 2,174 ⟶ 2,474:
=={{header|Prolog}}==
<
los(["AA","AA","AA"]).
los(["AA","CC","BB"]).
Line 2,194 ⟶ 2,494:
nl.
test :- forall(los(List), test_list(List)).</
{{out}}
<pre>
Line 2,222 ⟶ 2,522:
=={{header|PureBasic}}==
<
DataSection
Data.s ~"AA\tAA\tAA\nAA\tBB\tCC\nAA\tCC\tBB\nAA\tACB\tBB\tCC\nsingel_element"
Line 2,259 ⟶ 2,559:
PrintN("")
Next
Input()</
{{out}}
<pre>List : AA AA AA
Line 2,284 ⟶ 2,584:
A useful pattern is that when you need some function of an item in a list with its next item over possibly all items in the list then <code>f(a, nexta) for a, nexta in zip(alist, alist[1:]))</code> works nicely.
(Especially if an index is not needed elsewhere in the algorithm).
<
all(a < nexta for a, nexta in zip(strings, strings[1:])) # Strictly ascending
len(set(strings)) == 1 # Concise all equal
sorted(strings, reverse=True) == strings # Concise (but not particularly efficient) ascending
</syntaxhighlight>
Line 2,296 ⟶ 2,596:
and, if we wish, pass functional forms of standard operators to either of them:
<
Line 2,313 ⟶ 2,613:
all(map(lt, az, az[1:]))
)</
{{Out}}
<pre>True False True</pre>
Line 2,323 ⟶ 2,623:
The word <code>$></code> compares two strings using the QACSFOT lexical ordering. (QACSFOT - Quackery Arbitrary Character Sequence For Ordered Text. It is less arbitrary than the ASCII sequence.)
<
dup size 1 > while
behead swap
Line 2,337 ⟶ 2,637:
[ tuck $> if
[ dip not conclude ] ] ]
drop ] is allinorder ( [ --> b )</
=={{header|R}}==
We can test, first, whether all elements of vector `strings` are equal to the first element; and, second, whether the sorted order of the vector is equal to the original vector.
<syntaxhighlight lang="r">
all(strings == strings[1])
all(strings == sort(strings))
</syntaxhighlight>
Testing:
<syntaxhighlight lang="r">
manyStrings=list(
"a",
c("a", "b", "c"),
c("a", "c", "b"),
c("A", "A"),
c("a", "A"),
c(123, "A", "Aaron", "beryllium", "z"),
c(123, "A", "z", "Aaron", "beryllium", "z")
)
for (strings in manyStrings) {
print(strings)
print(all(strings == strings[1]))
print(all(strings == sort(strings)))
}
</syntaxhighlight>
Result:
<syntaxhighlight>
"a"
TRUE
TRUE
"a" "b" "c"
FALSE
TRUE
"a" "c" "b"
FALSE
FALSE
"A" "A"
TRUE
TRUE
"a" "A"
FALSE
TRUE
"123" "A" "Aaron" "beryllium" "z"
FALSE
TRUE
"123" "A" "z" "Aaron" "beryllium" "z"
FALSE
FALSE
</syntaxhighlight>
For `NULL` input returns `TRUE` to both tests, for all missing (`NA`) input returns `NA` to first test, `TRUE` to second.
=={{header|Racket}}==
Line 2,408 ⟶ 2,704:
Hence the wrapper in the code below:
<
(define ((list-stringX? stringX?) strs)
(or (null? strs) (null? (cdr strs)) (apply stringX? strs)))
Line 2,429 ⟶ 2,725:
(list-string<? '("a" "a")) => #f
(list-string<? '("a" "b" "a")) => #f
(list-string<? '("a" "b" "c")) => #t))</
=={{header|Raku}}==
(formerly Perl 6)
In Raku, putting square brackets around an [[wp:Infix_notation|infix]] operator turns it into a listop that effectively works as if the operator had been
<syntaxhighlight lang="raku"
[lt] @strings # Strictly ascending</
=={{header|Red}}==
<
list1: ["asdf" "Asdf" "asdf"]
Line 2,457 ⟶ 2,753:
print all-equal? list3
print sorted? list3
</syntaxhighlight>
{{out}}
<pre>false
Line 2,469 ⟶ 2,765:
=={{header|REXX}}==
===version 1===
<
* 28.06.2014 Walter Pachl
*--------------------------------------------------------------------*/
Line 2,511 ⟶ 2,807:
Say 'List' value(list)': neither equal nor in increasing order'
End
Return</
{{out}}
<pre>List ABC: elements are in increasing order
Line 2,523 ⟶ 2,819:
:::::::* '''parse upper arg x'''
:::::::* '''arg x'''
<
@.1= 'ayu dab dog gar panda tui yak' /*seven strings: they're all ascending.*/
@.2= 'oy oy oy oy oy oy oy oy oy oy' /* ten strings: all equal. */
Line 2,546 ⟶ 2,842:
if word(strings,k)<<=word(strings,k-1) then return 0 /*string>prev? */
end /*k*/ /* [↑] 0=false, [↓] 1=true. */
return 1 /*indicate that strings are ascending. */</
{{out|output|text= when using the supplied lists:}}
<pre>
Line 2,572 ⟶ 2,868:
===version 3===
This REXX version is more idiomatic.
<
@.1= 'ayu dab dog gar panda tui yak' /*seven strings: they're all ascending.*/
@.2= 'oy oy oy oy oy oy oy oy oy oy' /* ten strings: all equal. */
Line 2,590 ⟶ 2,886:
if how=='A' then if word(x,k) <<= word(x,k-1) then return 0 /*≤ prev.?*/
end /*k*/ /* [↓] 1=true. [↑] 0=false. */
return 1 /*indicate strings have true comparison*/</
{{out|output|text= is identical to the above REXX version.}} <br><br>
=={{header|RPL}}==
{{works with|HP|48G}}
≪ '''IF''' DUP SIZE 2 < '''THEN''' 1 '''ELSE''' ≪ == ≫ DOSUBS ΠLIST '''END'''
≫ ‘<span style="color:blue">ALLSAME?</span>' STO
≪ DUP SORT ==
≫ ‘<span style="color:blue">ALLORDERED?</span>' STO
=={{header|Ruby}}==
<
strings == strings.uniq.sort # ascending?</
Short circuiting:
<
strings.each_cons(2).all?{|str1, str2| str1 < str2} # ascending?</
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn strings_are_equal(seq: &[&str]) -> bool {
match seq {
&[] | &[_] => true,
&[x, y, ref tail @ ..] if x == y => strings_are_equal(&[&[y], tail].concat()),
_ => false
}
Line 2,617 ⟶ 2,918:
match seq {
&[] | &[_] => true,
&[x, y, ref tail @ ..] if x < y => asc_strings(&[&[y], tail].concat()),
_ => false
}
}</
=={{header|S-lang}}==
"Simple Loop" and "Array Idiomatic" versions:
<
{
variable n = length(sarr), a0, i;
Line 2,674 ⟶ 2,975:
atest(["single_element"]);
atest(NULL);
</syntaxhighlight>
{{out}}
<pre>"AA"
Line 2,712 ⟶ 3,013:
=={{header|Scala}}==
Functions implemented in Scala following a functional paradigm
<syntaxhighlight lang="scala">
def strings_are_equal(seq:List[String]):Boolean = seq match {
case Nil => true
Line 2,725 ⟶ 3,026:
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,751 ⟶ 3,052:
For known lists that are 'short-enough', the simplest solution uses 'apply', but that relies on the list being shorter than the maximum number of arguments a function can accept. Better is to write a simple loop:
<
(define (compare-strings fn strs)
(or (null? strs) ; returns #t on empty list
Line 2,763 ⟶ 3,064:
(compare-strings string=? strings) ; test for all equal
(compare-strings string<? strings) ; test for in ascending order
</syntaxhighlight>
=={{header|Seed7}}==
<
const func boolean: allTheSame (in array string: strings) is func
Line 2,792 ⟶ 3,093:
end if;
end for;
end func;</
=={{header|SenseTalk}}==
<
analyze ["AA","AA","AA"]
analyze ["AA","CC","BB"]
Line 2,818 ⟶ 3,119:
end repeat
return True
end isAscending</
{{Out}}
<pre>
Line 2,840 ⟶ 3,141:
=={{header|Sidef}}==
Short-circuiting:
<
1..arr.end -> all{ arr[_-1] < arr[_] } # strictly ascending</
Non short-circuiting:
<
arr == arr.uniq.sort # strictly ascending</
=={{header|Tailspin}}==
Note that we choose here to use 1 as true and 0 as false since Tailspin doesn't (yet?) have booleans
<
// matcher testing if the array contains anything not equal to the first element
templates allEqual
Line 2,873 ⟶ 3,174:
otherwise 0 !
end strictEqual
</syntaxhighlight>
=={{header|Tcl}}==
The command form of the <code>eq</code> and <code><</code> operators (introduced in Tcl 8.5) handle arbitrarily many arguments and will check if they're all equal/ordered.
Making the operators work with a list of values is just a matter of using the expansion syntax with them.
<
tcl::mathop::< {*}$strings; # All values in strict order</
=={{header|Transd}}==
<syntaxhighlight lang="Scheme">#lang transd
MainModule: {
_start: (λ
(for v in [["aa","ab","ad","ae"],["ab","ab","ab","ab"]] do
(lout :boolalpha v)
(lout (not (any v (λ (ret (neq @it (get v 0)))))))
(lout (not (any Range(in: v 1 -0)
(λ (ret (leq @it (get v (- @idx 1))))))) "\n")
)
)
}</syntaxhighlight>
{{out}}
<pre>
["aa", "ab", "ad", "ae"]
false
true
["ab", "ab", "ab", "ab"]
true
false
</pre>
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Private Function IsEqualOrAscending(myList) As String
Dim i&, boolEqual As Boolean, boolAsc As Boolean
Line 2,901 ⟶ 3,226:
IsEqualOrAscending = "List : " & Join(myList, ",") & ", IsEqual : " & (Not boolEqual) & ", IsAscending : " & Not boolAsc
End Function
</syntaxhighlight>
Call :
<syntaxhighlight lang="vb">
Sub Main()
Dim List
Line 2,915 ⟶ 3,240:
Debug.Print IsEqualOrAscending(List)
End Sub
</syntaxhighlight>
{{Out}}
<pre>
Line 2,928 ⟶ 3,253:
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Function string_compare(arr)
lexical = "Pass"
Line 2,952 ⟶ 3,277:
WScript.StdOut.WriteLine string_compare(Array("AA","ACB","BB","CC"))
WScript.StdOut.WriteLine string_compare(Array("FF"))
</syntaxhighlight>
{{Out}}
Line 2,976 ⟶ 3,301:
Ascending Test: Pass
</pre>
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn all_equal(strings []string) bool {
for s in strings {
if s != strings[0] {
return false
}
}
return true
}
fn all_less_than(strings []string) bool {
for i := 1; i < strings.len(); i++ {
if !(strings[i - 1] < s) {
return false
}
}
return true
}</syntaxhighlight>
=={{header|Wren}}==
{{libheader|Wren-sort}}
<
var areEqual = Fn.new { |strings|
Line 2,995 ⟶ 3,340:
System.print("%(b) are ascending : %(areAscending.call(b))")
System.print("%(c) are all equal : %(areEqual.call(c))")
System.print("%(d) are ascending : %(areAscending.call(d))")</
{{out}}
Line 3,003 ⟶ 3,348:
[a, a, b] are all equal : false
[a, d, c] are ascending : false
</pre>
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">include xpllib; \For StrCmp
func AreAllEqual(Strings, Size);
int Strings, Size, I;
[for I:= 1 to Size-1 do
if StrCmp(Strings(I), Strings(0)) # 0 then return false;
return true;
];
func AreAscending(Strings, Size;
int Strings, Size, I;
[for I:= 1 to Size-1 do
if StrCmp(Strings(I-1), Strings(I)) >= 0 then return false;
return true;
];
int A, B, C, D;
[A:= ["a", "a", "a"];
B:= ["a", "b", "c"];
C:= ["a", "a", "b"];
D:= ["a", "d", "c"];
Text(0, if AreAllEqual (A, 3) then "true" else "false"); CrLf(0);
Text(0, if AreAscending(B, 3) then "true" else "false"); CrLf(0);
Text(0, if AreAllEqual (C, 3) then "true" else "false"); CrLf(0);
Text(0, if AreAscending(D, 3) then "true" else "false"); CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
true
true
false
false
</pre>
=={{header|XProfan}}==
<
Parameters long liste
var int result = 1
Line 3,054 ⟶ 3,434:
ClearList 0
WaitKey
end</
{{out}}
<pre>
Line 3,076 ⟶ 3,456:
=={{header|zkl}}==
These short circuit.
<
fcn monoUp(strings){
strings.len()<2 or
strings.reduce(fcn(a,b){ if(a>=b) return(Void.Stop,False); b }).toBool()
}</
<
allEQ(T("AA","AA","AA","AA")).println(); //True
allEQ(T("A", "AA","AA","AA")).println(); //False
Line 3,088 ⟶ 3,468:
monoUp(T("a","aa","aaa","aaaa")).println(); //True
monoUp(T("a","aa","aaa","aaa")).println(); //False
monoUp(T("a","b","c","cc")).println(); //True</
=={{header|zonnon}}==
<
module CompareStrings;
type
Line 3,123 ⟶ 3,503:
write("ascending?: ");writeln(ascending)
end CompareStrings.
</syntaxhighlight>
=={{header|ZX Spectrum Basic}}==
{{trans|AWK}}
<
20 RESTORE j
30 READ n
Line 3,146 ⟶ 3,526:
180 DATA 3,"AA","CC","BB"
190 DATA 4,"AA","ACB","BB","CC"
200 DATA 1,"single_element"</
|