3 ms·
The Lil[0] scripting language, like most APL-derived languages, has uniform operator precedence; expressions evaluate right-to-left unless you introduce parens:
by RodgerTheGreat 3y ago
The Lil[0] scripting language, like most APL-derived languages, has uniform operator precedence; expressions evaluate right-to-left unless you introduce parens:
3 * 2 + 1
9
(3 * 2) + 1
7
Lil includes an integrated query syntax which loosely resembles SQL. Queries begin with a command (select, update, extract), contain intermediate clauses in any order (where, orderby, by), and conclude with "from":
select key value orderby value desc from x
The order of evaluation of clauses matches precedence of other operators: "from" executes, then every intermediate clause, right-to-left, then finally the command and any column expressions or aggregations. Since queries are expressions, rather than statements (like all control structures in lil), having "from" last means that you can chain "subqueries" without nesting them:
extract key orderby value desc from
select key:first value value:count value by value from a
I think this approach is nicely internally-consistent; the only real downside is that, as with SQL, this syntax is not ideal for IDE-driven auto-completion.
[0] https://beyondloom.com/tools/trylil.html https://beyondloom.com/tools/trylil.html
- dylan604 3y agoWTF is right-to-left evaluation's purpose? It breaks the most fundamental rules of arithmetic. As a human that has been taught those rules, I'd never come up with 9 as an answer. Why did humans create a programming language that would?
- RodgerTheGreat 3y agoTraditional mathematical notation has precedence rules optimized for expressing polynomials. Programming languages- especially in the APL family- tend to have a richer collection of primitive verbs, and an "ideal" tower of binding precedence for all of them would be extremely complex, difficult to remember, and often unhelpful. Unform evaluation order is much easier to remember and extend (no special cases!) and becomes even more natural than PEMDAS with a little practice. The APL family is hardly unique in this approach to precedence: Smalltalk has uniform infix precedence, Forths are uniformly postfix (unless you get goofy with parsing words), Lisps are uniformly prefix.
- fiddlerwoaroof 3y agoIMO, infix operators in math are a historical relic of function notation being developed after the basic operators became widespread. I’ve always thought uniform prefix notation is ideal, whether it’s lisp-style (+ 1 x y) or the more common +(1, x, y)
- fiddlerwoaroof 3y agoI remember having this question when learning J, and they pointed out that it makes the = operator behave correctly. After: a = 1 + 2 One expects `a` to equal 3. If you have a uniform left to right evaluation order, it will equal 1 and then the whole expression will be set to 3, so you’d need to write this to get the intuitive result: a = (1 + 2)