What's the reason for (/ num1 num2) rather than just num1/num2 for division for example?
What's the reason for (/ num1 num2) rather than just num1/num2 for division for example?
For example many lisps implement SRFI 105: Curly Infix 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 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?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.