Compare a list of strings: Difference between revisions
→{{header|R}}: Shorter and simpler
(→{{header|R}}: Shorter and simpler) |
|||
Line 2,520:
=={{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,530:
<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:
"a"
"a" "b" "c"
"a" "c" "b"
"A" "A"
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.
▲</syntaxhighlight>
▲[1] TRUE
▲[1] TRUE
▲[1] FALSE
▲[1] TRUE
▲[1] FALSE
▲[1] FALSE
▲[1] TRUE
=={{header|Racket}}==
|