Golfing APL/K in 90 Lines of Python
61 points
5 days ago
| 6 comments
| aljamal.substack.com
| HN
richard_todd
36 minutes ago
[-]
It's a fun article and this really doesn't matter much but `5{|+\x}\1,2` does not give the typical fibonacci sequence. Either `5{|+\x}\1,1` or `5{|+\x}\2,1` do, if the results from this random online interpreter can be believed (https://ngn.codeberg.page/k/#eJwzra7RjqmojTHUMQQAFyUDkw==).
reply
RodgerTheGreat
7 minutes ago
[-]
If one is golfing, tacit would be tidier:

    5(|+\)\1,1
reply
bbminner
57 minutes ago
[-]
I still consider jax.vmap to be a little miracle: fn2 = vmap(fn, (1,2)), if i remember correctly, traverces the computation graph of fn and correctly broacasts all operations in a way that ensures that fn2 acts like fn applied in a loop across the second dimension of the first argument (but accelerated, has auto-gradients, etc).
reply
Tiberium
2 hours ago
[-]
I find such "X in Y lines of code" challenges not very interesting most of the time, because, as it is the case here, they usually just pack multiple lines into one instead of using clever tricks, from one of the lines in that file:

> right, left = lambda f: lambda x, y: list(map(lambda yi: f(x, yi), y)) if not atom(y) else f(x, y), lambda f: lambda x, y: list(map(lambda xi: f(xi, y), x)) if not atom(x) else f(x, y)

reply
pjmlp
6 hours ago
[-]
The original Lisp in 1958 had only lists, by the 1970's many implementations already had all other key datastructures like arrays and hashes.
reply
forgotpwd16
6 hours ago
[-]
Partially reminds me (due to _V,_f,_F,f,F) Whitney's ksimple implementation[1].

[1]: https://github.com/kparc/ksimple/tree/main/ref#ac

reply
heliumtera
6 hours ago
[-]
I strongly dislike this choice of using all the symbols that do not exist on normal keyboards. I can't stand this, seems very attention seeking. Why not choose normal thingies that can be typed using the main interface we have with computers? This makes me mad, even.

But programs written in K are so beautiful and terse they are unlike anything else I've seen. It feels like there is something about it we can't really comprehend, like this beauty could not be achieve by accident, like there is something fundamentally right there...like there is some fundamental truth here. And maybe this is true about APL also.

reply
jasonwatkinspdx
5 hours ago
[-]
APL predates ASCII by a couple years.

It originally wasn't even intended as a software language, but rather a uniform mathematical notation in the style of curry's combinators, but more practical for describing non trivial algorithms.

So he was in an era where the expectation was if you were typesetting a mathematical monograph you'd already be doing stuff like swapping the balls on your IBM typewriter for math symbols.

It's not a choice you'd make today obviously, but it was entirely reasonable then.

As for why it persists, simple answer is APL fans like it that way. It's trivial to translate to some ascii text representation. I think anyone strongly motivated to do that just switched to j, k, or if even those are two weird goes to numpy or such.

reply
ofalkaed
4 hours ago
[-]
>It's not a choice you'd make today obviously, but it was entirely reasonable then.

More recently, BQN made this same choice and I think it is perfectly reasonable to do as long as you have a reason beyond simple aesthetics. Entering these symbols on a normal keyboard is not difficult and no different from learning a human language which uses a different alphabet than you keyboard.

Personally I find the custom symbols of APL and BQN to be easier to type and read than the ASCII of J and K.

reply
heliumtera
5 hours ago
[-]
>So he was in an era where the expectation was if you were typesetting a mathematical monograph you'd already be doing stuff like swapping the balls on your IBM typewriter for math symbols.

makes sense, maybe that would be more ergonomic to type for the public it targeted, indeed.

i won`t deny it is a stupid take of mine, but it makes me mad. i get the same feeling reading mathematical notations, so there is that.

reply
alt187
5 hours ago
[-]
To be fair, even after reading the other guy's post, I'm still mad about it. They even sell APL keyboards now. The indignity.
reply
KK7NIL
5 hours ago
[-]
> Why not choose normal thingies that can be typed using the main interface we have with computers?

Iverson answered this in his Turing Award acceptance lecture, which is literally linked in OP's article: https://www.eecg.utoronto.ca/~jzhu/csc326/readings/iverson.p...

You're free to disagree with him, but you need not wonder why!

reply
heliumtera
5 hours ago
[-]
thanks for the reference, appreciated
reply