
Sixteen APL Amuse-Bouches (2016) - kick
https://vector.org.uk/sixteen-apl-amuse-bouches/
======
jim-jim-jim
Forks and hooks interest me. I always thought they were a cute little feature
of APL and J, but I've actually found real world uses for them writing Haskell
at work. The little APL knowledge I've picked up has made me much more
comfortable with tacit programming in the latter language, probably to the
annoyance of my colleagues.

------
gorgoiler
I think I’m officially never going to be smart enough to _get_ APL. Perhaps
that day will come, but until then I find it hard to tell between these two
viewpoints:

(1) APL is another form of Perl golf, showing off basically. The fact that so
few people would be able to read it makes code essentially write-only and any
enthusiasm for it as a serious language for communicating ideas between
_people_ is quackery; or

(2) APL truly is readable, and if only I had received the right upbringing
from an early enough age then I would have a mind capable of becoming APL
enlightened, but it’s ok that I don’t because the virtue is that the next
generation, at least, will all be able to revel in the language’s brevity.
Just because I can’t look at _(+\%^,)•[x,}]•_ and instantly recognize it is a
JPEG parser, it doesn’t mean APL is a language without virtue.

Perhaps a better analogy would be with character languages (eg CJK) where it’s
quite apparent that billions of people function (no pun intended) productively
using them, and yet these writing systems will almost certainly remain forever
inscrutable to me.

(A counter-point to _that_ is that I believe character reading and writing
issues in terms of illegible handwriting are a much more significant societal
problem in China than they are in the West.)

~~~
masto
As I like to put it when I have to come up with a “fun fact” for corporate
icebreaker activities, “I have programmed in APL for money”. It’s not really
that hard.

The notation is certainly a stumbling block if you’re trying to read these
articles out of context. How do you even “read” something like “5⍴⍳20” in your
head when it’s that unfamiliar? But that’s the same as anything else, what
does “for (x = 0; x < 10; i++)” mean to someone who’s never seen it before? So
when you can see that as “5 rho iota 20”, at least you can pronounce it. But
when you’re actively using the language you know what ⍳20 means as easily as
you know what “1 through 20” means. “Reshape 5” is just as familiar. So it
takes only a glance at 5⍴⍳20 to visualize a 5x4 matrix of the numbers from 1
to 20.

Once you start thinking that way, the majority of what might seem like show-
off tricks or being clever are just the paradigm of the language. The idea of
creating a vector of things, applying a true/false test to every element to
create another vector of 1s and 0s, and summing those or using them to pick
elements out of another vector is a super powerful tool that lets you express
a lot of ideas in a compact form that describes the algorithm without getting
bogged down in writing explicit loops. What’s the average of these numbers?
The sum divided by the count. In APL you just write that down. In many other
languages, you write “I wish to declare a variable ‘i’ of type 32-bit-integer.
Set ‘i’ to 0. Compare ‘i’ to ...”. What does any of this have to do with an
average?

Now of course in an article where the author is actually showing off how
clever you can get, and the topic of many items is some obscure mathematical
concept, it’s going to be harder to read than the average program.

~~~
retrac
> The idea of creating a vector of things, applying a true/false test to every
> element to create another vector of 1s and 0s, and summing those or using
> them to pick elements out of another vector is a super powerful tool that
> lets you express a lot of ideas in a compact form that describes the
> algorithm without getting bogged down in writing explicit loops.

Exactly. APL's syntax and terminology is a bit odd. But what you're describing
there should be a concept known to anyone familiar with a functional language.
It seems you've just described what I would call mapping a function over an
array. Indeed, a very powerful construct, often lacking from more procedural
languages. Or, sadly, often eschewed by programmers even in the languages that
do have it.

APL makes me think of Forth. Because of its idiosyncratic terminology (e.g.
functions are "words") and unusual syntax (strict RPN), it is considered more
exotic than it really is.

~~~
icen
> But what you're describing there should be a concept known to anyone
> familiar with a functional language. It seems you've just described what I
> would call mapping a function over an array.

The other powerful notion is that arrays become mapping functions over their
index.

------
paulfurley
An APL is a very compact programming language, from Wikipedia:

> APL (named after the book A Programming Language)[2] is a programming
> language developed in the 1960s by Kenneth E. Iverson. Its central datatype
> is the multidimensional array. It uses a large range of special graphic
> symbols[3] to represent most functions and operators, leading to very
> concise code.

Took me some time to realise what I was even reading about, let alone the
content!

~~~
kick
"An APL is a very compact programming language" is grammatically incorrect.

"An A Programming Language is a very compact programming language."

As described by the ever-pleasant Dijkstra:

"APL is a mistake, carried through to perfection. It is the language of the
future for the programming techniques of the past"

~~~
uryga
hm. "An APL" sounds fine to me! i'd say that, grammatically, "APL" (and other
acronyms) become independent from their expansions.

e.g. would you say that expressions like "the FBI's policy" or "multiple DUIs"
are grammatically incorrect? expanding the acronym gives "The Federal Beaurau
of Investigations's policy" and "multiple Driving Under the Influences" which
sound wrong, but the original expression is fine.

~~~
kick
The FBI stands for "The Federal Bureau of Investigation." "The FBI's policy,"
as such, is grammatically correct.

English isn't a common language in Poland, is it?

~~~
uryga
my english is fine, thanks. my point was: splicing the expansion of "DUI" into
a sentence will often make it ungrammatical, but that doesn't matter, because
the acronym functions as a separate thing. analogously, expanding "an APL"
into "an <A Programming Language>" and calling it incorrect is a weird way to
judge a sentence.

anyway, i guess i was just in the mood to argue about something pointless
online! have a nice day :)

------
qznc
Looking at Advent of Code solutions, I'm not sure if APL is really so much
shorter or if it is rather a styling of programming.

Some day2 solutions from reddit [0]:

APL:

    
    
        0{⍺⋄s←⍵⋄c←s[f+⍳4]⋄99=0⌷c:0⌷s⋄i a b l←s[c]⋄s[3⌷c]←(0⌷c-1)⌷a(+,×)b⋄(⍺+4)∇s}{j←⍵⋄j[1 2]←12 2⋄j}⍎¨','(≠⊆⊢)(⎕NGET 'data')
    

Python:

    
    
        def R(I,x=12,y=2,O=__import__('operator')):
            q=p=[*map(int,I.split(','))];p[1:3]=x,y
            while p[0]!=99:o,a,b,c,*p=p;q[c]={1:O.add,2:O.mul}[o](q[a],q[b])
            return q[0]
        R(inp),next(100*x+y for x in range(100)for y in range(100)if R(I,x,y)==19690720)
    

K (APL derivative)

    
    
        f:** ({~99=x@y}/) {i:x@y+!4;n:@[x;i@3;$[1=*i;+;*]/x@i@1 2];(n;y+4)}//
        d1:@[.-1_1:`2.txt;1 2;:;12 2]
        f(d1;0)
        d2:{@[d1;1 2;:;x]}'z:+!2#100
        {y+100*x}/*z@&19690720=f'+(d2;0)
    

Another APL one:

    
    
        ⎕IO←0
        s←⍎¨','(≠⊆⊢)⊃⊃⎕NGET'p02.txt' 1
        f←{i←0⊣p[1 2]←⍵⊣p←s ⋄ ⊃p⊣{i+←4⊣a b c d←p[i+⍳4] ⋄ p[d]←(a-1)⌷p[b](+,×)p[c]}⍣{99=i⌷p}⊢0}
        f 12 2 ⍝ part 1
        +/100 1×∊{0::⍬ ⋄ 19690720=f⍵:⍵ ⋄ ⍬}¨1+,⍳99 99 ⍝ part 2
    

Ruby:

    
    
        def exec_op v { 1 => method(define_method(:a) {|x,y,z,m| m[z] = m[x] + m[y]}), 2 => method(define_method(:a) {|x,y,z,m| m[z] = m[x] * m[y]}) }[v] || method(define_method(:empty) {|a,b,c,d| nil }) end
        def run_program i i.each_slice(4).to_a.map {|p| exec_op(p[0]).call(p[1], p[2], p[3], i)} end
    

[0]
[https://old.reddit.com/r/adventofcode/comments/e4u0rw/2019_d...](https://old.reddit.com/r/adventofcode/comments/e4u0rw/2019_day_2_solutions/)

~~~
7thaccount
I think it makes better sense to compare how APL looks in the wild versus
Python/Ruby/Java whatever. Rosetta Code shows this pretty well. There are
dozens of examples where the APL program is one line and the
Python/Perl/Ruby/C#/Whatever is ~1/3 page if I recall correctly.

So that might prove your point better now that I think about it lol. Python
can be written like the user did above, but I've never written it that way or
seen anybody at work that has. Most of the APL I've seen (except for the
really old school kind) looks like oneliners.

~~~
Athas
Actually, my experience is that APL in the wild (as practiced by those who are
just trying to do their job) is not very terse at all. It looks like an old-
school procedural language. Since APL is an incredibly proprietary community,
it can be a bit hard to find public examples of "industrial" APL style, but
this looks similar to what I remember is written at places like SimCorp:
[https://github.com/Dyalog19/SA3/blob/953e591eace72bb1c147d19...](https://github.com/Dyalog19/SA3/blob/953e591eace72bb1c147d1914734a78d7af50baf/DUI/Core/JSON.dyalog)

------
sterlind
Why didn't Dijkstra like this?! I get that APL is an acquired taste but these
programs are beautiful. It pains me to read of such a visionary being so
close-minded to a radical and succinct way of expressing algorithms.

~~~
yiyus
[https://www.jsoftware.com/papers/Dijkstra_Letter.htm](https://www.jsoftware.com/papers/Dijkstra_Letter.htm)

~~~
qznc
J can be considered APL 2.0 and it fixes Dijkstras complaint since it is ASCII
only.

~~~
mlochbaum
The character set is not Dijkstra's complaint. His preferred language, ALGOL
60, also uses characters like × ÷ ≤ ¬ ⊃. ASCII wasn't really the definitive
standard it is now when Dijkstra wrote that APL was a "mistake" in 1975.

~~~
qznc
Then what does Dijkstra criticize?

> The fact that the printed or written word is apparently not the proper
> medium for the propagation of APL may offer a further explanation for its
> relative isolation; at the same time that fact may be viewed as one of its
> major shortcomings.

~~~
mlochbaum
Brace yourself. Dijkstra was complaining about the fact that APLers wanted a
computer available in order to write programs! He thought the only sufficient
way to develop programs was to first conceive a mathematical proof of the
program's correctness, then write the proof and the code together. I think
this is part of what he means when he talks about the future of computer
science, although it's not clear whether he actually thought this methodology
would become common. I probably have more sympathy for this viewpoint than
most, but I think it's obvious that it has not held up well.

Like many of Dijsktra's complaints about APL, this one is ironic because APL
today is one of the best embodiments of the idea that code should be
communicable without a computer. APLers will gladly discuss ideas with each
other by writing APL on paper or a blackboard, or even by speaking it out
loud. A few people have remarked that this is a unique feature of array
languages: other programmers will write things down, but only array
programmers use their own language to do so.

~~~
qznc
From Iversons autobiography
[https://www.jsoftware.com/papers/autobio.htm](https://www.jsoftware.com/papers/autobio.htm)

"To our surprise, the two teachers worked at the blackboard in their
accustomed manner, except that they used a mixture of APL and conventional
notation. Only when they and the class had worked out a program for some
matter in the text would they call on some (eager) volunteer to use the
terminal. The printed result was then examined; if it did not give the
expected result, they returned to the blackboard to refine it."

"... the initial motive for developing APL was to provide a tool for writing
and teaching. Although APL has been exploited mostly in commercial
programming, I continue to believe that its most important use remains to be
exploited: as a simple, precise, executable notation for the teaching of a
wide range of subjects."

The antipathy between Dijkstra and Iverson makes no sense to me. Was Dijkstra
just misinformed about APL?

~~~
kick
Most people are misinformed about APL, but Dijkstra took this to a spectacular
level.

------
GnarfGnarf
APL is like if we assigned a number to every word in the English language.
There are about 60,000 words: sixteen bits would do it. We could use base 64,
so every word would be reduced to three characters. Wouldn't that be an
improvement? </sarcasm>

