4 ms·
That's an example of what I call "stupid terse". When comparing program length, we should count tokens, not characters. We should give a penalty to excessivel
by kazinator 7y ago
That's an example of what I call "stupid terse".
When comparing program length, we should count tokens, not characters.
We should give a penalty to excessively long tokens, of course, like pointlessly_long_variable_or_function_names. Say, any token longer than 8 characters counts as two tokens. Any longer than 16 counts as 4 and so on, exponentially. Pairing tokens like { } and ( ) can count as one for the pair. In syntax like f(x, y), the () are essentialy one operator denoting function application, spread out to enclose the arguments.
Your APL
fib←{1≥⍵:⍵⋄(∇⍵-1)+∇⍵-2}
has 19 tokens. jodrelllblank's Python:
def fib(n):
if (1 >= n):
return n
else:
return fib(n-1) + fib(n - 2)
has 27. There is a bit of verbiage with the colons, the else punctuation and the explicit return commands.
TXR Lisp:
(defun fib (n) (if (plusp n) n (fib (+ (pred n) (ppred n)))))
has 21 tokens (or, if you will, nodes in the syntax tree). Only two more over APL, and provides a clear function definition from which we know it takes exactly one required argument, which is given a name. It exhibits no ambiguities.
- robocat 7y ago> When comparing program length, we should count tokens, not characters. GP said APL had "concision" and you then replied about measuring tokens, ended up using some personal ƒ(tokens,nodes,symbols) for reasons I can't fathom. > It exhibits no ambiguities What ambiguities are present in the APL function? If you answer, please write clearly, because I know nothing about APL (I was watching the video for my own didactic purposes). > That's an example of what I call "stupid terse". That seems needlessly provocative to me. Edits: clarity. I think I would like the function definition to be written a little more clearly, but I would need more interaction with APL authors to understand their communication norms for their language.
- kazinator 7y ago> What ambiguities are present in the APL function? For instance: ∇⍵-2 is that ∇(⍵-2) or (∇⍵)-2? If something similar to ∇⍵-2 appears but with another symbol in place of ∇, does it parse the same way? (I'm not even certain that the ∇⍵-2 excerpt is a syntactic sub-unit that is valid on its own.)
- rscho 7y agoInterestingly, you are right in that APL grammar is ambiguous, but not in the way you describe above. Programming in APL has been described as the "classical Chinese" of programming, and that's a good analogy. APL requires you to know both its primitives and implicit execution model off by heart to be efficient. You must learn to read it like a natural language, both vocabulary and grammar. By comparison, LISP has an explicit execution model in that it's explicitly reflected in the syntax by parentheses. Relations between APL primitives are implicit, and that's actually very nice and useful too!
- maest 7y agoFunnily enough, APL is as unambigous as possible when it comes to order of execution. It's _always_ right-to-left So ∇⍵-2 does ⍵-2 the applies ∇ to it. Simmilarly (and initially confusing), 2+45-2 evaluates to: (2+(4(5-2))) = 14 From experience, the simplicity of not having operator precendence is liberating (why do we even need operator precendence in the first place, really?).
- avmich 7y agoI'm sure in the numeric example the forum engine made the result wrong. Let me try. 2 + 4 * 5 - 2 should be 14...
- kazinator 7y agoIn HN, asterisks denote italics, which is troublesome for citing pieces of code in-line. Look at your rendered comment! Reliable code formatting is achieved by putting code in its own paragraph that has at least two leading spaces on each line.
- maest 7y agoOops, you're right. Unfortunately, it's too late to edit the post now (can't find an edit button).
- kazinator 7y agoWhy we need operator precedence is so that math notation can properly serve as a "tool of thought". The correspondence between arithmetic laws and an algebraic notation which remains sane when not fully parenthesized requires precedence levels. Higher "power" operations must be placed at higher precedence levels. For instance, given A(x + y + z) we can distribute A into the trinomial factor: Ax + Ay + Az That doesn't work if that result has some wacky interpretation like: A(x + A(y + (Az))) And under this rule, although you don't have precedence, you still have an implicit rule of associativity: multiplication and addition share the same precedence level, and associate right to left. You have not been been liberated from ambiguity that requires invisible rules. Why the higher-powered multiplication must have precedence over lower-powered addition is that at the most rudimentary level, it represents repeated addition. 3y means y + y + y arithmetically. If we want that equivalence to be mirrored in notation, we need the precedence. This allows us to take an expression like x + y + y + y + z and confidently rewrite y + y + y as 3y. The properties of addition and multiplication assure us that it's mathematically correct, and the properties of the syntax, assure us that the symbolic manipulation is semantics-preserving: replacing multiple terms of a sum by a single product has no interaction with adjacent terms due to precedence. That's the gist of it: correspondence between syntax rewrite rules and the semantics of algebraic rules.
- rscho 7y agoNow try to type both and see how much time it takes. Or even better, choose a math problem that makes you experiment and try to express it in both. If you try it long enough, you'll see why it's not stupid. APL is often described as a calculator, and at least to me it's exactly that. If you are programming math and even more so arrays, you can't beat array languages. I say that as a scheme fanboy, and I'm currently implementing J in Racket. Incidentally, Aaron Hsu is not only an APL expert but also a seasoned schemer, and it's not for nothing he came up with the "disposable code" idea. So very much not stupid. But as with LISP, you have put up a good try to see the value. Also, fib=:(-&2+&$:-&1)^:(1&<)M. Removing memoization and defining 'plusp', 'pred' and 'ppred' as you do, we can get it down to 10 tokens without any golfing, which is less than half your example for the same trivial program. And furthermore, fib"0 >: i. 4 5 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
- theamk 7y agoRe typing, I bet both Python and Lisp will be faster to type. I am typing English-like text all day already - in form of emails, papers, comments and so on, so I am proficient. Switching to exotic APL syntax will make me type much slower.
- rscho 7y agoWell, you should try it and see if you are right, then. APL really excels at quick experimentation for mathematically-oriented programs. Tacit J even more so (which is what I used above).
- jakeogh 7y agoI got fast at CTRL-m /frac /partial bla bla in LyX, how are APLers entering that unicode?... digging into the vids...
- jodrellblank 7y agoIt varies; they tend to follow to the keys on the early IBM dedicated keyboards with APL keys, styled like this: https://www.dyalog.com/uploads/images/Business/products/us_rc.jpg https://www.dyalog.com/uploads/images/Business/products/us_r... Nars2000 on Windows has its own application-specific key combos, with Alt+Key for the "lowercase" one and Alt+Shift+Key for the "uppercase" one. Dyalog APL, one of the popular commercial ones on Windows, installs a Windows system-wide input method which uses Ctrl+Key and Ctrl+shift+key; you can switch to it in any program. TryAPL.org uses a preceeding backtick so `w becomes omega, and there's a JavaScript bookmarklet which can add that to any text input on any browser page, here: https://abrudz.github.io/lb/apl https://abrudz.github.io/lb/apl (No idea what people are doing on *nix keyboard entry).
- nickpeterson 7y agoIt might interest you to read the thoughts of the author of APL, search for "notation as a tool of thought", it's the paper he won the Turing award for. Give it a read and see if it softens your resolve on this.
- jodrellblank 7y agoIt's all very well to say your lisp is clear and unambiguous, how long did it take you to realise it's wrong? It took me a good 20-30 minutes of writing a reply about character count, before I tried to format your lisp onto three lines and then noticed. I think the Python code is very clearly a correct Fibonacci, it looks at a glance like the layout of a procedural language doing a recursive call with a base case and a recursive case. The APL is not as clear in that sense, but knowing the diamond is a statement separator it breaks down into small pieces and is fairly easy to read because there's so little of it to read. The Lisp with 8 pairs of parens, 5 levels of nesting, and use of function names instead of "n - 2", while still being a long run of code where there are almost no symbols except alphabet letters and parens to make anything stand out, makes it difficult for me to parse by eye to see what the intended nesting is, and to make sure the parens are nested as intended. You have it returning fib ((n-1) + (n-2)) instead of ((fib (n-1))+(fib (n-2))), and I think you might have the condition the wrong way around, if N is positive then the recursive calls should happen. That's not to say you or Lisp can't write a correct Fib, it's to say that I think I would spot errors in the Python much more easily, thus making it "more clear". Which his another way of saying that I'm not convinced that counting characters is automatically worse than counting tokens - characters are what we type, what we have to read, they are what take up screen space and push other code off screen, they push related bits of code further apart even when it's all on-screen, and they are places bugs can hide. If they're not helping, what are they doing? Characters are the reason the APL could fit 3x in a single 80 character wide line, and the Lisp code 1.3x in the same line, and the Python code 0.2x in the same line. Not that I desperately want to read 80 characters of uncommented APL, but that adds up quickly. The APL tends to be high level and more declarative, which doesn't show up in Fibonacci, but makes a difference in character count. ---- APL functions in the dfn style (curly braces) can only take an argument on the left and an argument on the right, both optional. The left one becomes alpha ⍺ and the right one becomes omega ⍵. That is to say, because you see omega in the code, you know it too expects exactly one argument, that argument goes on the right, and it has a name. The name is no less meaningful than "n", although no more meaningful either. The steep learning curve about APL code isn't "what does the code say, those symbols are unreadable", they stop being scary quite quickly and just stay dense, instead it's "how on Earth does that manipulation of arrays solve the problem??"
- dang 7y ago> "stupid terse" Please don't break the HN guideline about calling names. Especially not with the shallowest of all programming dismissals (the old canard about the APLs, tied for first place with "but those parens" about the Lisps). https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html