3 ms·
Some of the syntax choices seem a little strange to me, but I maybe just don't understand the language. What's the reason for (/ num1 num2) rather than just nu
by Closi 2y ago
Some of the syntax choices seem a little strange to me, but I maybe just don't understand the language.
What's the reason for (/ num1 num2) rather than just num1/num2 for division for example?
- dunefox 2y ago... because it's a lisp?
- satvikpendem 2y ago`/` is just a function, there aren't operators as something special above functions. This is because it is a Lisp, and even this syntax is just a generalization of Polish notation [0]. [0] https://en.wikipedia.org/wiki/Polish_notation https://en.wikipedia.org/wiki/Polish_notation
- antiquark 2y agoCall me old fashioned, but I always thought syntax like "x = a * b + c" was a leap forward.
- digitalsankhara 2y agoI am old. And fashioned. "a b * c +" rules! :-)
- whartung 2y agoTo a point, but at a cost. In C++, there's 17 levels of precedence across 60 operators, with varying associativity. In many languages, due to precedence, 1 + 2 * 3 = 7, but in others, like Smalltalk, it equals 9. In those languages, its purely left to right, with no precedence. But at a casual glance, they look identical. And for someone bouncing back and forth, they better ensure they have the right hat on when they start writing out equations. In prefix languages, this is not an issue. Everything looks like a function. +, -, sqrt, sin, draw. You also can have niceties like (+ 1 2 3 4), which simply adds up all of the arguments, in contrast to (+ 1 (+ 2 (+ 3 4))). But, that's not different grammatically from (sin (sqrt (abs x))), (f1 (f2 (f3 x))), which appears like sin(sqrt(abs(x))) in infix languages, not much different really at all. Clearly there are benefits to infix presentation, and for basic (or, even BASIC!) use its very approachable. But underlying it, there's some definite complexity that you need to grok when the equations start to get complicated.
- Y_Y 2y agoA lot of people have asked the same question over the years, and by now there are some good answers. For example many lisps implement SRFI 105: Curly Infix https://srfi.schemers.org/srfi-105/ https://srfi.schemers.org/srfi-105/ which lets you write {a * b + c / d} instead of (+ (* a b) (/ c d)). Afaik Fennel doesn't have this, but that's fine because it has macros! (macro infix [expr] (let [stack [] ops { "+" (fn [a b] (+ a b)) "-" (fn [a b] (- a b)) "*" (fn [a b] (* a b)) "/" (fn [a b] (/ a b))}] (fn [item] (if (number? item) (table.insert stack item) (let [op (get ops item) b (table.remove stack -1) a (table.remove stack -1)] (table.insert stack (op a b))))) (each [_ v (ipairs expr)] (infix v)) (first stack))) And now you can do: (infix [1 + 2 * 3 - 4 / 5]) ; => 5.2
- peeters 2y agoI can't quickly parse this in a way that makes 5.2 1 + (2 * 3) - (4 / 5) = 6.2 (PEMDAS) 1 + (2 * (3 - (4 / 5))) = 5.4 (greedy) (((1+2) * 3) - 4) / 5 = 0.4 (non-greedy) Was it a typo or is it using some other grouping I don't understand?
- deleted 2y ago[deleted]
- Y_Y 2y agoTypo, I should have actually run the code! That macro doesn't have any fancy operator precedence rules, but you could certainly add them.
- agent281 2y agoThere is a whole branch of programming languages that use prefix notation like that. It's one of the oldest branches starting in 1958. The benefit of the prefix notation is that it is really easy to write macros and embed Domain Specific Languages (DSLs). It's an interesting part of the programming world. I would recommend checking it out! https://en.wikipedia.org/wiki/Lisp_(programming_language) https://en.wikipedia.org/wiki/Lisp_(programming_language)
- deleted 2y ago[deleted]