Compare a list of strings: Difference between revisions
m
→{{header|Bruijn}}
(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
<syntaxhighlight lang="elena">import system'collections;
import system'routines;
Line 975 ⟶ 1,029:
{
isEqual()
= nil == self.seekEach(self.FirstMember, (n,m => m
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 -
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:
<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.
<syntaxhighlight lang="r">
all(strings == strings[1])
all(strings == sort(strings))
</syntaxhighlight>
Line 2,535 ⟶ 2,651:
<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,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
<syntaxhighlight lang="raku" line>[eq] @strings # All equal
Line 2,771 ⟶ 2,888:
return 1 /*indicate strings have true comparison*/</syntaxhighlight>
{{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}}==
Line 3,199 ⟶ 3,324:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="
var areEqual = Fn.new { |strings|
|