Hacker News new | ask | show | jobs
by beagle3 29 days ago
APL / K / J (and friends) evaluate things "left-of-right" (could be thought of as "right-to-left"), that is 1+23-4 is parsed 1+(2(3-4)) - there is no operator precedence. It is weird at first, but refreshingly simple and effective once you manage to overcome indoctrination you received since elementary school.
1 comments

Well maybe it's just the stuff I do but even if you do something like 'address + offset * scale'

I still want to group that to make it obvious 'address + (offset * scale)'

The former technically works but it's less obvious. And that's a dead simple example

'address + offset << scale' is notionally the same but << has a lower precedence in c than + so you get the opposite behaviour so it's better to just bracket it out of habit.

I never find that I'm adding 3 random numbers. I'm adding 2 random numbers to make a meaningful value, then adding another number so a lot of the time I do 'foo + (bar + baz)'

Maybe if you're into pure mathematics you have a different take, but in my experience it's often the case that you end up bracketing most mathmatical operations for readability anyway

What you seem to be saying is “I want no precedence or associativity - I just want explicit parentheses everywhere” which is a fine choice. But not a common one.

The reason APL made that choice (and its descendants followed) is that it had many tens of operators. There’s no intuitive or otherwise accepted order among them. Which means it’s either all parentheses (like you seem to prefer) or a simple rule (left-of-right). There’s basically no other solution.

Forth and Lisp also dropped precedence and associativity rules, each in their own way.