User:Devon McCormick/RightToLeftAdvantages
Advantages of Evaluating Expressions from Right to Left
Here's a lightly edited summary of the advantages of right-to-left evaluation sent by Jack Andrews <effbiae@gmail.com> to chat@jsoftware.com on July 20, 2008. This summary is extracted from Elementary Functions An Algorithmic Treatment (pdf) which was written by Ken Iverson in 1966.
The reasons for a right-to-left instead of a left-to-right convention are: 1. The usual mathematical convention of placing a monadic function to the left of its argument leads to a right-to-left execution for monadic functions; for example, F G x = F (G x) (see note [0] below) 2. The notation F/ z for reduction (by any dyadic function F) tends to require fewer parentheses with a right-to-left convention. For example, expressions such as +/ (x * y) [1] or +/ (u/x) tend to occur more frequently than (+/ x) * y and (+/ u) / x. 3. An expression /evaluated/ from right to left is easiest to /read/ from left-to-right. For example, the expression a + x * b + x * c + x * d + x * e + x * f (for the efficient evaluation of a polynomial) is read as "a" plus the entire expression following, or as "a" plus "x" times the following expression, or as "a" plus "x" times "b" plus the following expression, and so on.
Note that one would seldom write out an expression like this in preference to the more compact notation e.g.
Coeff +/ . * |:X^/i.6 _192 _14.16832 1.30816 4.34304 36.71552 268 }}} where the sample values might be as follows:
Coeff=. 2 3 5 4 1 6 ]X=. i:2j5 _2 _1.2 _0.4 0.4 1.2 2
Checking the equivalence of these two expressions:
'a b c d e f'=. Coeff x=. X a + x * b + x * c + x * d + x * e + x * f _192 _14.16832 1.30816 4.34304 36.71552 268 }}} we see that they give the same result though the former version is simpler to write and more easily generalized while the latter version illustrates the details of the underlying evaluation.
4. In the definition F/ x = x1 F x2 F x3 F ... F xn (see notes [0][2] below) the right-to-left convention leads to a more useful definition for nonassociative functions F than does the left-to-right convention. For example, -/ x denotes the alternating sum of the components of x, whereas in a left-to-right convention it would denote the first component minus the sum of the remaining components. Thus if d is the vector of decimal digits representing the number n, then the value of the expression 0 = 9|+/ d determines the divisibility of n by 9; in the right to left convention, the similar expression 0 = 11|-/ determines divisibility by 11.
As pointed out by R. E. Boss, the mathematical expression a^b^c^d (generally) is interpreted as ^/a,b,c,d . "The convention for iterated exponentiation is to work from the right to the left." as noted here . This exception is a (rare) recognition of the greater usefulness of a right-to-left evaluation since working in the other direction would cause repeated exponentiation to evaluate as the less interesting a^(b*c*d) .
Notes
[0] the symbol '=' here means congruence (triple horizontal lines in the original) [1] the symbol '*' here and later means multiplication ('x' in the original) [2] the 'n' in xn is denoted {rho} x in the original