X

#### Xah Lee

A excerpt from the new book ã€ˆModern Perlã€‰, just published, chapter 4

on â€œOperatorsâ€. Quote:

Â«The associativity of an operator governs whether it evaluates from

left to right or right to left. Addition is left associative, such

that 2 + 3 + 4 evaluates 2 + 3 first, then adds 4 to the result.

Exponentiation is right associative, such that 2 ** 3 ** 4 evaluates 3

** 4 first, then raises 2 to the 81st power. Â»

LOL. Looks like the perl folks haven't changed. Fundamentals of

serious math got botched so badly.

Let me explain the idiocy.

It says â€œThe associativity of an operator governs whether it evaluates

from left to right or right to left.â€. Ok, so let's say we have 2

operators: a white triangle â–³ and a black triangle â–². Now, by the

perl's teaching above, let's suppose the white triangle is â€œright

associativeâ€ and the black triangle is â€œleft associativeâ€. Now, look

at this:

3 â–³ 6 â–² 5

seems like the white and black triangles are going to draw a pistol

and fight for the chick 6 there. LOL.

Now, let me tell you what operator precedence is. First of all, let's

limit ourselfs to discuss operators that are so-called binary

operators, which, in our context, basically means single symbol

operator that takes it's left and right side as operands. Now, each

symbol have a â€œprecedenceâ€, or in other words, the set of operators

has a order. (one easy way to think of this is that, suppose you have

n symbols, then you give each a number, from 1 to n, as their order)

So, when 2 symbols are placed side by side such as ã€Œ3 â–³ 6 â–² 5ã€, the

symbol with higher precedence wins. Another easy way to think of this

is that each operator has a stickiness level. The higher its level, it

more sticky it is.

the problem with the perl explanations is that it's one misleading

confusion ball. It isn't about â€œleft/right associativityâ€. It isn't

about â€œevaluates from left to right or right to leftâ€. Worse, the word

â€œassociativityâ€ is a math term that describe a property of algebra

that has nothing to do with operator precedence, yet is easily

confused with because it is a property about order of evaluation. (for

example, the addition function is associative, meaning: ã€Œ(3+6)+5 =

3+(6+5)ã€.)

compare it with this:

ã€ˆPerl ï¼† Python: Complex Numbersã€‰

http://xahlee.org/perl-python/complex_numbers.html

and for a good understanding of functions and operators, see:

ã€ˆWhat's Function, What's Operator?ã€‰

http://xahlee.org/math/function_and_operators.html