78 points · by emersonrsantos · 7y ago · 51 comments
https://www.youtube.com/results?search_query=aaron+hsu
It's a bit caustic to the mainstream (beauty, terseness over ease and variable names) but I personally find it tickles all my boxes.
Commercialization of video has ruined this art form. Just compare early Mythbuster episodes to something from 2016. Production quality has horrendously degraded. The money is where the masses are and the masses want entertainment, not education.
As a minor taste of the power of direct fns, here's a naive implementation of the Fibonacci sequence with one: `fib←{1≥⍵:⍵⋄(∇⍵-1)+∇⍵-2}`
This fib code in the parent comment translates to:
def fib(n):
if (1 >= n):
return n
else:
return fib(n-1) + fib(n - 2)
and it does so with: fib ← { "scriptblock" function declaration
implicit variable name ⍵ (omega) for the right side argument
1≥⍵ : ⍵ guard case, if 1>=⍵, return ⍵
⋄ statement separator, like ; in C-languages
if no guard clauses matched, remaining code runs
∇ recursive call to the function it appears in
∇(⍵-1)+∇⍵-2 fib (⍵-1) + fib (⍵-2)
}
More notably, the kind of tacit programming support looks like this: (××∘⌊|+1≤2||) N Rounding to nearest even number (favouring away from 0)
which evaluates to a function that takes N on the right, and the tacit train looks like this, from TryAPL.org visualization: ┌─┼────┐
× ∘ ┌─┼───┐
┌┴┐ | + ┌─┼───┐
× ⌊ 1 ≤ ┌─┼─┐
2 | | <- N goes in here
The tree evaluates from the lower right. Each triple is a fork, N goes into the right prong, the left prong, then the result of both of those goes into the center prong. Unless there's a constant on the left prong, then that's used as-is. So:|N for the magnitude; 2|(that) for the remainder of dividing by 2; 1<=(that) to find whether to add 0 or 1 for the rounding, (|N)+(that) to re-do the magnitude of N and add the 0 or 1. xN to get the direction +1 or -1, multiplied by the floor of the calculated result.
N enters in several places, which avoids having to store temporary variables and name them, when you aren't actually interested in new variables.
And this works on a single number, a vector or multi-dimensional matrix of numbers.
The {} indicate that this is a function definition: https://www.gnu.org/software/apl/apl.html#Section-3_002e7
My initial take on this, at least, is that it seems pretty reader-hostile. Maybe you get used to it, though.
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.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 6765GP 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.
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.)
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).
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??"
Yes I do! I did this just minutes before rushing out the door, and didn't even bother to run it.
Now, the funny thing is, I was focused on paraphrasing your Python expression for expression, so that it calculates Fibonacci in exactly the same way with the same conventions, so any size comparisons are fair. (Though obviously I took advantage of the pred and ppred idioms.) I wasn't thinking "is this correct Fib", but "does it match whatever the Python is doing".
So now I'm looking at: why did I transcribe the simple conditional backwards? Taking a second look at the Python, I now clearly see the reversed convention in 1 >= n that I read as n >= 1.
Exactly. The visual cortex easily takes care of the readability question, given time and proper exposure for training.
Thinking of how to solve problems using matrices and indices is a whole 'nother thing. It's quite fun if you're bent that way.
(I think; I've never used it)
This is (to me) even more apparent in his next language, J; where he sought to "correct" deficiencies in APL and further expanded the "language" to include things like "gerunds"[1] more explicitly, which are nominally (as defined in an English dictionary):
> a noun formed from a verb, denoting an action or state
Which may be summarized somewhat unsatisfactorily as: "Because Ken said so".
[0]: https://www.jsoftware.com/papers/tot.htm
[1]: https://code.jsoftware.com/wiki/Vocabulary/GerundsAndAtomicR...