Notes on the APL programming language
As stated before, I have a fetish for unconventional programming languages. I recently came across APL (acronym for “a programming language”), which is both unconventional and mind-blowing in a sense.
It was originally intended to be a mathematical notation for manipulating arrays, but just like Lisp it ended up being a programming language when IBM implemented it in the late 60s.
Even-thought APL is still very much alive, hiding in dusty corners of banking-software code-basis out there, I think it’s fair to say it has fell out of fashion. However, there’s still a lot of value in learning it.
For starters, APL was hugely influential in the design of languages such as C++, MATLAB or Python. It’s creator, Kenneth E. Iverson even won a Turing Award for his work in APL. APL is indeed a relic of the past, but it’s vector-based semantics can still teach us new ways to understand computation and it’s historical value is unquestionable. As Perlis once said,
A language that doesn’t affect the way you think about programming, is not worth knowing.
and I assure you APL will affect the way you think about programming.
The Language
The first thing you need to know about APL is that it’s syntax has multiple
mathematical symbols, such as ←
for assignment and ⍝
for comments. This
makes it a pain to program in, since you have to keep copying and pasting
obscure Unicode characters around to get anything done. It also makes APL very
hard to read — let’s just say APL code is quite concise.
The only data structure APL supports is the vector (i.e. nonempty arrays of
numbers). Vectors can be be written inline or constructed with the iota (⍳
)
operator, which returns a vector of indices:
⍝ Inline vector
1 6 3 ⍝ Outputs 1 6 3
⍝ Vector of indices
⍳5 ⍝ Outputs 1 2 3 4 5
Vectors can be folded with the /
operator, which takes a binary operator and
a list:
⍝ Computes the sum of the integers between 1 and 5.
+/⍳5 ⍝ Outputs 15
Vectors of the same length can be combined with any binary operation (which works like zip-with):
⍝ Adding two vectors
1 3 5+2 4 6 ⍝ Outputs 3 7 11
Likewise, unary operators can be applied to vectors:
⍝ Vector of multiplicative inverses
÷1 3 5 ⍝ Outputs 1 0.3333333333 0.2
This is about everything you need to know about APL — as far as I can tell,
having programmed in it for about 2 hours. APL supports some other fun stuff,
such as custom monadic (single argument) and dyadic (two arguments) operators,
lambdas (anonymous functions) and mapping constructs (the “over-each” or ¨
operator), but I believe I’ve covered the bulk of it in here.
Learning & Using APL
Most of my knowlodge about APL comes from the Wikipedia page on APL syntax. I’d recommend starting with that. You could also read the original paper on APL and A Practical Introduction to APL 1 & APL 2.
If you want to run APL code in your machine I’d suggest using GNU APL, which is a free & open-source interpreter.