all tasks

This commit is contained in:
Ingy döt Net 2013-04-11 01:07:29 -07:00
parent b83f433714
commit 68f8f3e56b
14735 changed files with 178959 additions and 0 deletions

View file

@ -0,0 +1,15 @@
{{basic data operation}}[[Category:Basic language learning]]The task is to demonstrate how to compare two strings from within the language and how to achieve a lexical comparison. The task should demonstrate:
* Comparing two strings for exact equality
* Comparing two strings for inequality (i.e., the inverse of exact equality)
* Comparing two strings to see if one is lexically ordered before than the other
* Comparing two strings to see if one is lexically ordered after than the other
* How to achieve both case sensitive comparisons and case insensitive comparisons within the language
* How the language handles comparison of numeric strings if these are not treated lexically
* Demonstrate any other kinds of string comparisons that the language provides, particularly as it relates to your type system. For example, you might demonstrate the difference between generic/polymorphic comparison and coercive/allomorphic comparison if your language supports such a distinction.
Here "generic/polymorphic" comparison means that the function or operator you're using doesn't always do string comparison, but bends the actual semantics of the comparison depending on the types one or both arguments; with such an operator, you achieve string comparison only if the arguments are sufficiently string-like in type or appearance. In contrast, a "coercive/allomorphic" comparison function or operator has fixed string-comparison semantics regardless of the argument type; instead of the operator bending, it's the arguments that are forced to bend instead and behave like strings if they can, and the operator simply fails if the arguments cannot be viewed somehow as strings. A language may have one or both of these kinds of operators; see the Perl 6 entry for an example of a language with both kinds of operators.
'''See also:'''<!-- Note that this part might go away, as it is handled by the “basic data operation” template -->
* [[Integer comparison]]
* [[Character matching]]

View file

@ -0,0 +1,2 @@
---
note: Basic Data Operations

View file

@ -0,0 +1,17 @@
BEGIN {
a="BALL"
b="BELL"
if (a == b) { print "The strings are equal" }
if (a != b) { print "The strings are not equal" }
if (a > b) { print "The first string is lexically after than the second" }
if (a < b) { print "The first string is lexically before than the second" }
if (a >= b) { print "The first string is not lexically before than the second" }
if (a <= b) { print "The first string is not lexically after than the second" }
# to make a case insensitive comparison convert both strings to the same lettercase:
a="BALL"
b="ball"
if (tolower(a) == tolower(b)) { print "The first and second string are the same disregarding letter case" }
}

View file

@ -0,0 +1,9 @@
10 LET "A$="BELL"
20 LET B$="BELT"
30 IF A$ = B$ THEN PRINT "THE STRINGS ARE EQUAL": REM TEST FOR EQUALITY
40 IF A$ <> B$ THEN PRINT "THE STRINGS ARE NOT EQUAL": REM TEST FOR INEQUALITY
50 IF A$ > B$ THEN PRINT A$;" IS LEXICALLY HIGHER THAN ";B$: REM TEST FOR LEXICALLY HIGHER
60 IF A$ < B$ THEN PRINT A$;" IS LEXICALLY LOWER THAN ";B$: REM TEST FOR LEXICALLY LOWER
70 IF A$ <= B$ THEN PRINT A$;" IS NOT LEXICALLY HIGHER THAN ";B$
80 IF A$ >= B$ THEN PRINT A$;" IS NOT LEXICALLY LOWER THAN ";B$
90 END

View file

@ -0,0 +1,3 @@
10 LET A$="BELT"
20 LET B$="belt"
30 IF UPPER$(A$)=UPPER$(B$) THEN PRINT "Disregarding lettercase, the strings are the same."

View file

@ -0,0 +1,8 @@
blsq ) "abc""abc"==
1
blsq ) "abc""abc"!=
0
blsq ) "abc""Abc"cm
1
blsq ) "ABC""Abc"cm
-1

View file

@ -0,0 +1,23 @@
import std.stdio, std.string, std.algorithm;
void main() {
auto s = "abcd";
/* Comparing two strings for exact equality */
assert (s == "abcd"); // same object
/* Comparing two strings for inequality */
assert(s != "ABCD"); // different objects
/* Comparing the lexical order of two strings;
-1 means smaller, 0 means equal, 1 means larger */
assert(s.icmp("Bcde") == -1); // case insensitive
assert(s.cmp("Bcde") == 1); // case sensitive
assert(s.icmp("Aabc") == 1); // case insensitive
assert(s.cmp("Aabc") == 1); // case sensitive
assert(s.icmp("ABCD") == 0); // case insensitive
assert(s.cmp("ABCD") == 1); // case sensitive
}

View file

@ -0,0 +1,6 @@
eq=: -: NB. equal
ne=: -.@-: NB. not equal
gt=: {.@/:@,&boxopen *. ne NB. lexically greater than
lt=: -.@{.@/:@,&boxopen *. ne NB. lexically less than
ge=: {.@/:@,&boxopen +. eq NB. lexically greater than or equal to
le=: -.@{.@/:@,&boxopen NB. lexically less than or equal to

View file

@ -0,0 +1,6 @@
'ball' (eq , ne , gt , lt , ge , le) 'bell'
0 1 0 1 0 1
'ball' (eq , ne , gt , lt , ge , le) 'ball'
1 0 0 0 1 1
'YUP' (eq , ne , gt , lt , ge , le) 'YEP'
0 1 1 0 1 0

View file

@ -0,0 +1,23 @@
animal = 'dog'
if animal = 'cat' then
say animal "is lexically equal to cat"
if animal \= 'cat' then
say animal "is not lexically equal cat"
if animal > 'cat' then
say animal "is lexically higher than cat"
if animal < 'cat' then
say animal "is lexically lower than cat"
if animal >= 'cat' then
say animal "is not lexically lower than cat"
if animal <= 'cat' then
say animal "is not lexically higher than cat"
/* The above comparative operators do not consider
leading and trailing whitespace when making comparisons. */
if ' cat ' = 'cat' then
say "this will print because whitespace is stripped"
/* To consider all whitespace in a comparison
we need to use strict comparative operators */
if ' cat ' == 'cat' then
say "this will not print because comparison is strict"

View file

@ -0,0 +1,36 @@
sub compare($a,$b) {
my $A = "{$a.WHAT.^name} '$a'";
my $B = "{$b.WHAT.^name} '$b'";
if $a eq $b { say "$A and $B are lexically equal" }
if $a ne $b { say "$A and $B are not lexically equal" }
if $a gt $b { say "$A is lexically after $B" }
if $a lt $b { say "$A is lexically before than $B" }
if $a ge $b { say "$A is not lexically before $B" }
if $a le $b { say "$A is not lexically after $B" }
if $a === $b { say "$A and $B are identical objects" }
if $a !=== $b { say "$A and $B are not identical objects" }
if $a eqv $b { say "$A and $B are generically equal" }
if $a !eqv $b { say "$A and $B are not generically equal" }
if $a before $b { say "$A is generically after $B" }
if $a after $b { say "$A is generically before $B" }
if $a !after $b { say "$A is not generically before $B" }
if $a !before $b { say "$A is not generically after $B" }
say "The lexical relationship of $A and $B is { $a leg $b }" if $a ~~ Stringy;
say "The generic relationship of $A and $B is { $a cmp $b }";
say "The numeric relationship of $A and $B is { $a <=> $b }" if $a ~~ Numeric;
say '';
}
compare 'YUP', 'YUP';
compare 'BALL', 'BELL';
compare 24, 123;
compare 5.1, 5;
compare 5.1e0, 5 + 1/10;

View file

@ -0,0 +1,17 @@
def compare(a, b):
print("\n%r is of type %r and %r is of type %r"
% (a, type(a), b, type(b)))
if a < b: print('%r is strictly less than %r' % (a, b))
if a <= b: print('%r is less than or equal to %r' % (a, b))
if a > b: print('%r is strictly greater than %r' % (a, b))
if a >= b: print('%r is greater than or equal to %r' % (a, b))
if a == b: print('%r is equal to %r' % (a, b))
if a != b: print('%r is not equal to %r' % (a, b))
if a is b: print('%r has object identity with %r' % (a, b))
if a is not b: print('%r has negated object identity with %r' % (a, b))
compare('YUP', 'YUP')
compare('BALL', 'BELL')
compare('24', '123')
compare(24, 123)
compare(5.0, 5)

View file

@ -0,0 +1,23 @@
animal = 'dog'
if animal = 'cat' then
say animal "is lexically equal to cat"
if animal != 'cat' then
say animal "is not lexically equal cat"
if animal > 'cat' then
say animal "is lexically higher than cat"
if animal < 'cat' then
say animal "is lexically lower than cat"
if animal >= 'cat' then
say animal "is not lexically lower than cat"
if animal <= 'cat' then
say animal "is not lexically higher than cat"
/* The above comparative operators do not consider
leading and trailing whitespace when making comparisons. */
if ' cat ' = 'cat' then
say "this will print because whitespace is stripped"
/* To consider all whitespace in a comparison
we need to use strict comparative operators */
if ' cat ' == 'cat' then
say "this will not print because comparison is strict"

View file

@ -0,0 +1,16 @@
#lang racket
;; Comparing two strings for exact equality
(string=? "foo" "foo")
;; Comparing two strings for inequality
(not (string=? "foo" "bar"))
;; Comparing two strings to see if one is lexically ordered before than the other
(string<? "abc" "def")
;; Comparing two strings to see if one is lexically ordered after than the other
(string>? "def" "abc")
;; How to achieve both case sensitive comparisons and case insensitive comparisons within the language
(string-ci=? "foo" "FOO")

View file

@ -0,0 +1,9 @@
a$ = "dog"
b$ = "cat"
if a$ = b$ then print "the strings are equal" ' test for equalitY
if a$ <> b$ then print "the strings are not equal" ' test for inequalitY
if a$ > b$ then print a$;" is lexicallY higher than ";b$ ' test for lexicallY higher
if a$ < b$ then print a$;" is lexicallY lower than ";b$ ' test for lexicallY lower
if a$ <= b$ then print a$;" is not lexicallY higher than ";b$
if a$ >= b$ then print a$;" is not lexicallY lower than ";b$
end

View file

@ -0,0 +1,6 @@
if {$a eq $b} {
puts "the strings are equal"
}
if {$a ne $b} {
puts "the strings are not equal"
}

View file

@ -0,0 +1,6 @@
if {[string compare $a $b] < 0} {
puts "first string lower than second"
}
if {[string compare $a $b] > 0} {
puts "first string higher than second"
}

View file

@ -0,0 +1,3 @@
if {[string equal -length 3 $x "abc123"]} {
puts "first three characters are equal"
}

View file

@ -0,0 +1,18 @@
#!/bin/sh
A=Bell
B=Ball
# Traditional test command implementations test for equality and inequality
# but do not have a lexical comparison facility
if [ $A = $B ] ; then
echo 'The strings are equal'
fi
if [ $A != $B ] ; then
echo 'The strings are not equal'
fi
# All variables in the shell are strings, so numeric content cause no lexical problems
# 0 , -0 , 0.0 and 00 are all lexically different if tested using the above methods.
# However this may not be the case if other tools, such as awk are the slave instead of test.