[cyjpchvk] Commutative syntactically first when equal precedence

October 10, 2019

Infix expressions which contain both pluses and minuses (addition and subtraction) are confusing: a+b - c+d - e-f (we've exacerbated the problem here with confusing/deceptive spacing).  (Previously, on forbidding such expressions.)  Even though such expressions are technically well defined by left associativity, consider restricting such expressions only to those in which all the pluses to come first, then all the minuses, e.g., a + b + d - c - e - f.  This allows the idiom of BIG - small - small - small..., a main term followed by a series of subtractive corrections.  The main term consists of addition only.  It is also kind of reminiscent of Lisp in which the first term in a s-expression is special (the function).

Similarly A * B * C / x / y / z.

In general, uses of commutative operator (+ *) come first.

However, once you've taken the effort to group the subtractions together, it's not much more effort to restrict subtraction to purely binary (no associativity) and subtract the parenthesized sum, e.g., a + b + d - (c + e + f) and A * B * C / (x * y * z) .

We unfortunately can't write alternating series the traditional way, but maybe that's for the best: the traditional way is full of opportunities for error.  Create some dedicated syntax for specifically for alternating series.

Even if a compiler isn't going to impose these restrictions on expressions, one could adopt them as coding style.

Share this

Related Posts

Previous
Next Post »