2^3^4 - a comparison of math packages
plus.google.com
plus.google.com
2+2x2 I can see (Smalltalk has strict left-to-right evaluation of operators, so it parses as (2+2)x2), but for 2x2+2 to give the "wrong" would require `+` to have a higher precedence than `*`, something I've yet to see (short of abusing haskell to redefine the operators with fucked up precedences)
edit: god fucking dammit HN can't you get any markup right?
On a related note, in Excel, -1^2 = 1 because they are calculating (-1)^2 instead of the more standard interpretation, -(1^2).
Seeing the example really makes me wonder why that convention exists, and also makes me ponder whether having variate associativity is actually a good idea after all. Especially in languages like Scala, where you can change the associativity of your binary functions through function naming.
Though I can see why things like the cons operator "::" make a lot of sense.
4
3
2
You should also try to visualize the 4 smaller and 3, and 3 smaller than 2. Because this is the way it was written for a long time.Prefix notation doesn't automatically solve the problem, you need to assume an evaluation order. More specifically, you need to not assume associativity.
For left associativity (uncommon):
^ ^ 2 3 4
For right associativity (common): ^ 2 ^ 3 4
Of course, all this is assuming all operators are fixed arity, otherwise you need parentheses no matter what. (a^b)^c = a^(b.c)
(using dot for multiplication because I fear that HN will mess up an asterisk)So, a right associative exponentiation makes your expression language more powerful.
? (expt 2 3 4)
> Error: Too many arguments in call to #<Compiled-function EXPT #x3000000A6C2F>:
> 3 arguments provided, at most 2 accepted. ? (expt (expt 2 3) 4)
4096
? (reduce 'expt '(2 3 4))
4096
? (expt 2 (expt 3 4))
2417851639229258349412352
? (expt 3 (expt 4 5))
373391848741020043532959754184866588225409776783734007750636931722079040617265251229993688938803977220468765065431475158108727054592160858581351336982809187314191748594262580938807019951956404285571818041046681288797402925517668012340617298396574731619152386723046235125934896058590588284654793540505936202376547807442730582144527058988756251452817793413352141920744623027518729185432862375737063985485319476416926263819972887006907013899256524297198527698749274196276811060702333710356481