5
$\begingroup$

On the subject of associativity when all operators have equal precendence, Ken Iverson writes in Conventions Governing Order of Evaluation that

The reasons for choosing 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) .
  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) or +/(u/x) tend to occur more frequently than (+/x)×y and (+/u)/x .
  3. An expression evaluated from right to left is the 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.
  4. In the definition
    F/x ≡ x1 F x2 F x3 F ... F x⍴x
    
    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|-/d determines divisibility by 11 .

What would be the reasons for choosing a left-to-right convention?

$\endgroup$
4
  • 3
    $\begingroup$ Most choices regarding language conventions comes down to "it's what users are now used to". $\endgroup$ Commented May 17, 2023 at 12:55
  • $\begingroup$ @chrisaycock Sure, but that doesn't mean no reasons exist that are disconnected from what people are used to. In this paper, Iverson broke with the traditional precedence he had adhered to in his previous writings. $\endgroup$
    – Adám
    Commented May 17, 2023 at 12:57
  • 1
    $\begingroup$ Smalltalk uses left-to-right evaluation for everything, even when you wouldn’t think the operators have equal precedence (e.g. 1 + 2 * 3 is 9). Would an explanation of that be relevant here? $\endgroup$
    – Bbrk24
    Commented May 17, 2023 at 12:58
  • 3
    $\begingroup$ @Bbrk24 Smalltalk's reasons for that would be relevant. $\endgroup$
    – Adám
    Commented May 17, 2023 at 13:01

2 Answers 2

5
$\begingroup$

Consistency with pure OO

In Smalltalk, everything is a method (even operators), and all methods that take more arguments than just self use infix syntax. Using a simple left-to-right rule solidifies this consistency, and avoids having to implement precedence for regular methods.

Obviously, this has the drawback of running counter to mathematical convention, when 1 + 2 * 3 is 9 rather than 7.

$\endgroup$
8
  • $\begingroup$ So, one-argument methods/operators are postfix? $\endgroup$
    – Adám
    Commented May 17, 2023 at 13:04
  • 1
    $\begingroup$ Correct, assuming you’re counting self in the argument count. $\endgroup$
    – Bbrk24
    Commented May 17, 2023 at 13:06
  • $\begingroup$ OK, but then having one-argument methods/operators be prefix would be equally consistent with right-to-left. What reason is there to go suffix, then? $\endgroup$
    – Adám
    Commented May 17, 2023 at 13:11
  • $\begingroup$ Many OO langs treat the self/this argument separately — it’s no different than C++ doing foo.bar() instead of bar(foo). $\endgroup$
    – Bbrk24
    Commented May 17, 2023 at 13:12
  • $\begingroup$ Sure, but isn't ()bar.foo equally sensible? $\endgroup$
    – Adám
    Commented May 17, 2023 at 13:13
4
$\begingroup$

Easier writing

In my experience writing APL programs, I constantly have to press the left arrow key to type in the next part of my program. A left-to-right evaluation would would effectively reverse that, allowing for a more natural flow of program writing. Note that this would apply more to symbolic languages like APL and golfing languages, instead of conventional C derivatives.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .