Compare a list of strings: Difference between revisions

→‎{{header|Bracmat}}: Made a small simplification of the code and added explanation.
(→‎{{header|Bracmat}}: Added Bracmat example)
(→‎{{header|Bracmat}}: Made a small simplification of the code and added explanation.)
Line 441:
 
=={{header|Bracmat}}==
Some explanation of the tests:
<lang Bracmat> (test1=first.~(!arg:%@?first ? (%@?:~!first) ?))
 
& (test2=x.~(!arg:? %@?x (%@?:~>!x) ?))</lang>
<code>test1</code> and <code>test2</code> are functions that return their input, but, more importantly, either succeed or fail.
 
<code>first</code> and <code>x</code> are local variables in <code>test1</code> and <code>test2</code>, respectively.
 
The bodies of the two functions consist of pattern matching operations that either succeed or fail. The pattern matching operator is the colon <code>:</code>. This operator, like all Bracmat's operators, is binary. The operand on the left hand side is the subject, the operand on the right hand side is the pattern.
 
The symbols <code>?</code>, <code>!</code>, <code>%</code>, <code>@</code>, <code>&gt;</code>, and <code>~</code> are prefixes.
 
<code>?</code> when prefixed to a symbol like <code>first</code> or <code>x</code>, makes the symbol a variable that receives the value of the subject or of part of te subject, without constraining what can be received. When prefixed to a zero length symbol (the empty string), it matches anything, like a wildcard.
 
<code>!</code> when prefixed to a symbol like <code>first</code> or <code>x</code>, evaluates to the value that was bound to the symbol. So it complements the <code>?</code> prefix. A symbol is a variable if and only if it is prefixed with <code>?</code> or <code>!</code>.
 
<code>%</code> is a prefix that modifies a pattern component such that it can match one or more elements from the subject. So it is more restrictive than <code>?</code>.
 
<code>@</code> is a prefix that modifies a pattern component such that it can match zero or one elements from the subject. So it is (much) more restrictive than <code>?</code>. The combination <code>%@</code> means: this subpattern can only match exactly one element.
 
<code>&gt;</code> is a prefix that modifies a pattern component to only match values that are greater than the value of the pattern component.
 
<code>~</code> is a prefix used to negate what comes after it. In <code>test1</code>, the first <code>~</code> negates the outcome of a pattern matching operation. In the subpattern <code>~!first</code> it says: match anything that is not the value of <code>!first</code>. In <code>~&gt;!x</code> it is negates the prefix <code>&gt;</code>. Together, <code>~></code> means: "not greater than" or, equivalently, "less than or equal to".
 
If a pattern match operator occurs inside a pattern as in <code>%@:~>!x</code>, then both operands are patterns. So this expression is to be read as:"match exactly one element of the subject and require that it is less than or equal to the value of <code>x</code>".
 
In words, the tests do the following: test1 assigns the first element of the argument to the "first" and then looks for another element that is not equal to "first". If the search succeeds, test1 fails and if the search fails, test1 succeeds. Test2 searches for two consecutive elements where the second element is not greater than the first elemnt. If the search succeeds, test2 fails and if the search fails, test2 succeeds.
 
 
<lang Bracmat> (test1=first.~(!arg:%@?first ? (%@?:~!first) ?))
& (test2=x.~(!arg:? %@?x (%@?:~>!x) ?))</lang>
 
Demonstration
483

edits