Compare a list of strings: Difference between revisions

m
 
(11 intermediate revisions by 9 users not shown)
Line 677:
test1 lstC succeeds
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}}==
Line 965 ⟶ 987:
true
}</syntaxhighlight>
 
=={{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 56.0x :
<syntaxhighlight lang="elena">import system'collections;
import system'routines;
Line 975 ⟶ 1,029:
{
isEqual()
= nil == self.seekEach(self.FirstMember, (n,m => m.equal:n.Inverted != n));
isAscending()
Line 984 ⟶ 1,038:
later.next();
^ nil == former.zipBy(later, (prev,next => next <= prev )).seekEach::(b => b)
}
}
Line 998 ⟶ 1,052:
public program()
{
testCases.forEach::(list)
{
console.printLine(list.asEnumerable()," all equal - ",list.isEqual());
Line 1,011 ⟶ 1,065:
AA,BB,CC ascending - true
AA,AA,AA all equal - true
AA,AA,AA ascending - falsetrue
AA,CC,BB all equal - false
AA,CC,BB ascending - false
Line 1,149 ⟶ 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: httphttps://www.forthforth2020.org/beginners-to-forth/a-novice.html-package
<syntaxhighlight lang="forth">
: test-equality ( string node -- new-string bad? )
Line 1,265 ⟶ 1,319:
 
{{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}}==
Line 2,520 ⟶ 2,641:
=={{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.
Let's start with a function that splits a vector into
sub-vectors; it starts a new vector whenever the comparison
function yields false.
 
<syntaxhighlight lang="r">
all(strings == strings[1])
chunks <- function (compare, xs) {
all(strings == sort(strings))
starts = which(c(T, !compare(head(xs, -1), xs[-1]), T))
lapply(seq(1,length(starts)-1),
function(i) xs[starts[i]:(starts[i+1]-1)] )
}
</syntaxhighlight>
 
Line 2,535 ⟶ 2,651:
 
<syntaxhighlight lang="r">
manyStrings=list(
> chunks(`<`, c(0,4,8,1,3,5,7,9))
"a",
[[1]]
c("a", "b", "c"),
[1] 0 4 8
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) {
[[2]]
print(strings)
[1] 1 3 5 7 9
print(all(strings == strings[1]))
print(all(strings == sort(strings)))
}
</syntaxhighlight>
 
Result:
R displays the results in a very prolix manner, so let's simplify it.
<syntaxhighlight>
 
"a"
<syntaxhighlight lang="r">
TRUE
> toString(chunks(`<`, c(0,4,8,1,3,5,7,9,-2,0,88)))
TRUE
[1] "c(0, 4, 8), c(1, 3, 5, 7, 9), c(-2, 0, 88)"
"a" "b" "c"
> toString(chunks(`==`, c(0,0,0,5,5,8)))
FALSE
[1] "c(0, 0, 0), c(5, 5), 8"
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.
Defining the required functions:
 
<syntaxhighlight lang="r">
all.eq <- function(xs) 1 == length( chunks(`==`, xs))
ascending <- function(xs) 1 == length( chunks(`<`, xs))
</syntaxhighlight>
 
Testing:
 
<syntaxhighlight lang="r">
> all.eq(c('by'))
[1] TRUE
> all.eq(c('by','by','by'))
[1] TRUE
> all.eq(c('by','by','by','zoo'))
[1] FALSE
> ascending(c("at", "even", "once", "run", "zoo"))
[1] TRUE
> ascending(c("at", "even", "once", "run", "zoo", "we"))
[1] FALSE
> ascending(c("at", "even", "go", "go"))
[1] FALSE
> ascending(c("at"))
[1] TRUE
</syntaxhighlight>
 
=={{header|Racket}}==
Line 2,613 ⟶ 2,730:
(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 butput in between all of the elements of the argument list ''(or in technical terms, it [[wp:Fold_(higher-order_function)|folds/reduces]] the list using that operator, while taking into account the operator's inherent [https://design.raku.org/S03.html#Operator_precedence associativity] and identity value)'':
 
<syntaxhighlight lang="raku" line>[eq] @strings # All equal
Line 2,771 ⟶ 2,888:
return 1 /*indicate strings have true comparison*/</syntaxhighlight>
{{out|output|text=&nbsp; 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}}==
Line 3,199 ⟶ 3,324:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "./sort" for Sort
 
var areEqual = Fn.new { |strings|
55

edits