Re: [xsl] Imagine that the semantics of comparison operations was this

Subject: Re: [xsl] Imagine that the semantics of comparison operations was this
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 2 Mar 2025 17:07:29 -0000
> <xsl:variable name="isDifferent" select="$FIRST ne $SECOND"
as="xs:boolean"/>
>   <xsl:variable name="APPEND" as="xs:string?">
>       <xsl:if test="$isDifferent eq true()">
>           <xsl:sequence select="concat($FIRST, $SECOND)"/>
 >    </xsl:if>
> </xsl:variable>

A single XPath expression, probably shorter than the SNOBOL one:

($first || $second)[$first ne $second]

or even:

$first || $second[. ne $first]


Also, please note that while XSLT allows arbitrarily complex definitions
for a variable, a developer can (and should) avoid writing:

  <xsl:if test="$isDifferent eq true()">, which is an obfuscated version of:

  <xsl:if test="$isDifferent">

Anyway, if this must be done in XSLT, I would probably do:

<xsl:variable name="append" select="($first || $second)[$first ne
$second]"/>

And I would even avoid creating such a variable if its value is used only
once in the code and the meaning of the containing expression is clear.

> Lessons Learned: how we define the semantics of comparison operations can
have a profound influence on programming style. Thus, if you are creating a
new programming > language, be aware that there are other ways to define
the semantics of comparison operations-you might decide to define the
semantics as XSLT/XPath does--true or false--> but alternatively, you might
decide to define the semantics as SNOBOL does-terminate the statement or
null string.

SNOBOL is a child of the pre-OOP (not even mention FP) era, in the 50-ies
and 60-ies of the 20th century. It was normal at that time - and even later
- to trace "program execution" command by command, left to right, and to
reflect this way of execution into the language that was developed.

The features of a new programming language, including comparisons, would
reflect the guiding philosophy and goals, and could be expected to be
different in procedural and declarative languages.

Thanks,
Dimitre.


On Sun, Mar 2, 2025 at 7:24b/AM Roger L Costello costello@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Hi Folks,
>
> Here is XPath that compares two string variables to determine if they
> differ:
>
> $FIRST ne $SECOND
>
> The result of evaluating the expression is true or false.
>
> The programming language called String Oriented Symbolic Language (SNOBOL)
> can also compare two string variables to determine if they differ. SNOBOL
> has a built-in function-DIFFER-for this. Here's how the comparison is
> expressed:
>
> DIFFER(FIRST, SECOND)
>
> If the FIRST and SECOND variables are not different, no value is returned
> and statement execution terminates. If FIRST and SECOND are different, then
> the null string is returned. That later part (returning a null string) is
> interesting because null strings usually vanish from computations without
> having any effect. The typical example is concatenation of the null string
> onto another string. The result is the same as if the null string had not
> been there. For this reason, a comparison operation can be inserted into an
> expression conditional on the success of the comparison operation.
>
> As an example of this, suppose we want to do this: If FIRST and SECOND are
> different, then assign to variable APPEND the concatenation of FIRST and
> SECOND. If FIRST and SECOND are not different, then leave APPEND
> unaffected. Here's how to do it in SNOBOL:
>
> APPEND = DIFFER(FIRST, SECOND) FIRST SECOND
>
> If FIRST and SECOND differ, then DIFFER(FIRST,SECOND) returns the null
> string and APPEND is assigned the concatenation of the null string, the
> value of FIRST, and the value of SECOND. In SNOBOL, concatenation is such a
> common operation that simply juxtaposing two strings means concatenation.
> If FIRST and SECOND do not differ, then execution of the statement
> containing DIFFER(FIRST,SECOND) terminates and APPEND is unaffected.
>
> To achieve the same functionality with XSLT and XPath, we would do this:
>
> <xsl:variable name="isDifferent" select="$FIRST ne $SECOND"
> as="xs:boolean"/>
> <xsl:variable name="APPEND" as="xs:string?">
>     <xsl:if test="$isDifferent eq true()">
>         <xsl:sequence select="concat($FIRST, $SECOND)"/>
>     </xsl:if>
> </xsl:variable>
>
> Lessons Learned: how we define the semantics of comparison operations can
> have a profound influence on programming style. Thus, if you are creating a
> new programming language, be aware that there are other ways to define the
> semantics of comparison operations-you might decide to define the semantics
> as XSLT/XPath does--true or false--but alternatively, you might decide to
> define the semantics as SNOBOL does-terminate the statement or null string.
>
> /Roger
>
> P.S. I got the information about SNOBOL from the wonderful book, "A
> SNOBOL4 Primer" by Ralph E. Griswold and Madge T. Griswold, pages 18-19.
> Some of the above sentences are excerpts from the book.

Current Thread