4 ms·
Nice to see a "brain dump" as someone who learned many languages and can easily relate with most of the issues the author faced. But I take issue with this:
by brabel 5y ago
Nice to see a "brain dump" as someone who learned many languages and can easily relate with most of the issues the author faced.
But I take issue with this:
// Obviously good and easy to read
return a*(1.0-t) + b*t;
// Obviously bad and hard to read
return add(mul(a, 1.0 - t), mul(b, t));
Sorry, but the first one is not obviously good, it's just what you're used to (the second one is indeed bad).
Here's what I would consider obviously good :) for the objective reason that it does not require difficult to track implicit rules about operator precedence:
1, - t, * a, + (t, * b)
I invented this syntax myself :) but it's extremely obvious once you know how it works.
It should be clear that this is similar to concatenative languages, where you push values onto a stack then apply operations on the stack. The `,` is used for pushing to the stack, basically. But then, it also allows you to "mix" more standard function notation into it... so when you write `a b` that means calling the `a` function with `b` as an argument (think LISP).
Now putting everything together
1, - t
Should read as `1 minus t` as `- t` is the function `-` being called with one value from the stack, `1`, and one from the "function call" (as if it was LISP `(- 1 t)`).
Next:
1, - t, * a
Reads as "1 minus t times a" and has no ambiguity: the result of `1, - t` is the first argument to ``, the second argument is `a`, so you get LISP's `( (- 1 t) a)` but reads much more naturally.
Finally, at the end, for readability, we use parens to group the final part of the equation:
1, - t, * a, + (t, * b)
Hopefully it's obvious what happens now?
Does anyone like this form of expression or is just me? I tried to mix the best of LISP with the best of FORTH :) and I really like this.
- alexvoda 5y agoThe notation the author complains about looks like a broken polish (prefix) notation with added parens. Your notation looks like a very bizare mix of infix arrangement postfix (RPN) behaviour plus added parens for good measure. RPN has plenty of benefits, once you get used to its initial awkwardness, that I feel your notation fails to capitalise on.
- ReleaseCandidat 5y ago> The notation the author complains about looks like a broken polish (prefix) notation with added parens. The notation the author complains about is the first version using 'normal' (prefix) functions. a + b = (+)(a, b) = add(a, b) = a `add` b (Haskell infix) = a .add. b (Fortran infix) a * b = (*)(a, b) = mul(a, b) = a `mul` b = a .mul. b so a*(1.0-t) + b*t is (+)(a*(1.0-t), b*t) = (+)((*)(a, 1.0-t), (*)(b, t)) = add(mul(a, 1.0-t), mul(b, t))
- rak1507 5y agoI think there's a compromise between being recognisable as a mathematical formula and removing operator precedence which is what APL and other array languages do. (a×1-t)+b×t is how you'd write it in APL, no operator precedence but still more familiar.
- user-the-name 5y agoThat absolutely breaks the law of least astonishment. It's worse to look familiar but have hidden differences than to just look completely different.
- rak1507 5y agoYeah maybe, but in my opinion it's better than the alternative (having loads of operator precedence rules that can change subtly between languages and never really being sure of anything).
- littlestymaar 5y ago> Sorry, but the first one is not obviously good, it's just what you're used to. That's also what everybody who as been in primary school is used to. Infix notation have its quirks but at least its ubiquitous.
- ReleaseCandidat 5y ago> That's also what everybody who as been in primary school is used to. That would (hopefully ;) have been (1 - t)a + tb
- thom 5y agoThe LISP version can be more readable with (e.g. Clojure’s) threading macros: (-> 1 (- t) (* a) (+ (* t b))) Obviously use more or less whitespace to taste.
- user-the-name 5y agoI can not read that at all.
- thom 5y agoOn the premise that someone might write LISP for a living, I'm saying it can help clarify a multistep mathematical expression. It's incredibly tedious to relitigate people's feelings about brackets over and over again.
- edflsafoiewq 5y agoI think it actually obscures the important facts about the expression by breaking up the structure which is displayed by the usual infix notation 1. It is a linear combination of a and b: there are two symmetric terms, a scaled copy of a and a scaled copy of b 2. The factors are functions of t alone which goes between 0 and 1 as t does This isn't litigating about brackets, the traditional prefix notation also displays this structure (though less well than the infix notation IMO) (+ (* a (- 1 t)) (* b t))
- thom 5y agoI was really only interested in the similarity between the threaded form and the proposal with all the commas (since they mentioned LISP) but I wish I'd had an aneurysm and died before sending the comment tbh.
- edflsafoiewq 5y agoYeah, sorry, didn't mean to pile on. I guess my thoughts re the OP are that the common notation for addition and multiplication is not just "what you're used to", but is connected to objective mathematical properties (associativity, commutativity, distributivity).
- edflsafoiewq 5y agoA notation which everyone is already used to is obviously good. A very compelling benefit is needed to outweigh "you already understand this".
- Zababa 5y ago(a * (1 - t)) + (b * t) would be my favorite way to express this in a programming context, I don't really see the point of your proposition over prefix or stack: (+ (* a (- 1 t)) (* b t)) 1 t - a * b t * +
- nemothekid 5y ago>Sorry, but the first one is not obviously good, it's just what you're used to (the second one is indeed bad). The first one is good because its what most people are used too - there's little that can be objectively said about notation once you reach a big enough audience. I could invent a word editor that produced documents from top to bottom rather then left to right and I could invent a million reasons what it's better, but that hardly matters. Personally I don't think Polish notation is all that revolutionary for humans, it's just easier for computers; however just like anything else the human brain is quite malleable, and if you spend a year doing equations in polish notation you will come to prefer it.
- kazinator 5y agoIt has been proposed to me (just yesterday!) by a collaborator to ad a linear interpolation function to TXR Lisp, so that would look like: (lerp t a b) ;; possibly, modulo argument order.