Let's Learn A Programming Language (APL) - Part 1
Now that we are all stuck in our homes in front of the computer, looking for new skills to add to our arsenal, what’s better than learning A Programming Language ?
What do you ask? Which programming language?
I told you — A Programming Language.
Still don’t get it? I see, let’s rephrase — “Let’s Learn APL”
Seriously, APL is an acronym for “A Programming Language”. This has to be the laziest name in CS history.
— “So I have made this programming language. What should I name it?”
— “How about A Programming Language”
— “Oh my God, you’re so brilliant! Who says programmers suck at names?”
(Actually APL was named after the book “A Programming Language” by Kenneth Iverson)
Anyway, let’s start, but why APL?
Well you see, APL is very interesting language. It’s concise, it’s powerful, and most importantly if you write APL around your friends, it can make you look 1337.
Here’s an example from Wikipedia —
life←{↑1 ⍵∨.∧3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵}
This is a program for “Conway’s Game of Life”
Also from Wikipedia —
APL is used for many purposes including financial and insurance applications, artificial intelligence, neural networks and robotics. It has been argued that APL is a calculation tool and not a programming language; its symbolic nature and array capabilities have made it popular with domain experts and data scientists who do not have or require the skills of a computer programmer.
APL is well suited to image manipulation and computer animation, where graphic transformations can be encoded as matrix multiplications. One of the first commercial computer graphics houses, Digital Effects, produced an APL graphics product named Visions, which was used to create television commercials and animation for the 1982 film Tron. Latterly, the Stormwind boating simulator uses APL to implement its physics engine.
Today, APL remains in use in a wide range of commercial and scientific applications, for example investment management, asset management, health care, and DNA profiling, and by hobbyists.
Also, I have seen many C++, Java, Python, JS and other tutorials. So, I thought of doing something different. Going a route where others don’t generally tread. Will you get a high profile job if you learn APL? I don’t know, but it doesn’t hurt to learn a new language, does it?
Well, APL will.
Just kidding. Please don’t get afraid. I need your views :3
Ok, so I have divided this guide into some parts. I have no idea how many parts there will be. This part deals with installing and getting around APL.
Also a disclaimer: I am no expert in APL. I’m learning APL too. This will be a journey of all of us, together. Also, I use Linux. If you use Windows, or MacOS, you might face some problems.
Let’s start.
Now APL has an interesting syntax. It doesn’t only use standard symbols, but has a plethora of other unicode symbols. for example ⍺<≤⍋⍒⍫ are all valid symbols in APL. That’s why APL requires a different keyboard layout.
Generally speaking, Dyalog is most probably the most popular APL installation out there. But I couldn’t get it to run on my Arch Linux. So I’ll be using GNU APL instead. If you’re a Windows user, and want to use Dyalog, you should follow the setup guide on the Dyalog website, and configure the keyboard. Here, I’ll be describing the configuration process for Linux.
Or, if you don’t want to go into the hassle of installing APL, visit https://tryapl.org/ to try out online APL.
First let’s install GNU APL. On my arch linux, it would be sudo pacman gnu-apl
Now that it’s installed, let’s set up the keyboard. Thankfully all modern Linux distros come with APL keyboard preinstalled. We will use the Dyalog layout. I’ll also use the right Alt as the switch key, so that pressing and holding the right alt will produce APL syntax.
I use KDE, so here’s what I did —
First go to System Settings -> Input Devices -> Keyboards -> Layout
Click on Add -> Select “Any language” from “Limit selection by language”, select “APL” from “Layout”, select “Dyalog APL Complete” from “Variant”, give it a label — I gave “apl” and select OK
Then select the new added layout, and press “Move Up” to make it your 2nd preferred layout.
Now at the top right, click on “Main shortcuts” and select “right Alt (while pressed)”
Now apply the settings, and when you type normally, you’ll see your normal input, but when you press and hold right alt and type you’ll see the APL syntax. For example, if I press the equals key, it inputs =. If I press right alt+equals, it inputs ÷
If you don’t use KDE, you’ll have to resort to whatever DE you use, or just use the terminal. Take a look at this answer. Make sure to choose right alt as the modifier.
Now that we are ready to go, let’s start with some basic APL stuff.
open a terminal and type apl
You’ll see this screen
Since this is an interpreter, we’ll write an APL command, press Enter, and it will show the result. The gray area at the bottom is the input prompt.
Let’s start by doing some basic stuff.
Add the prompt, type 3
and press enter. You’ll see 3 as a result.
Cool, so if you enter a number, it just evaluates it an echoes it back. Let’s try something else — enter 1 2 3
(note: they’re space separated)
As you can see, it returns the same thing. But here, it’s an array
. The concept of array is kinda similar to other languages. In APL, arrays are the building blocks of representing data.
Now let’s try something else. From now on, I won’t show you the screenshot. I’ll follow the convention of displaying input ant output in APL style, where the input starts 6 characters indented from the left margin, and output starts from the left margin.
Now enter a
You should see
VALUE ERROR
a
^
Since the program doesn’t know what a
is supposed to be, it generates an error.
Let’s try some arithmetic.
Enter 2+3
2+3
5
As you can see, adding works fine. And so does subtraction.
2-3
¯1
The “high” minus sign stands for the minus sign before negative numbers. This distinguishes between negative numbers and subtraction operator.
Let’s try multiplication and division. Unlike other languages *
and /
do not stand for multiplication and division, in APL, we use × and ÷ for them. If you are using Dyalog key layout (and right alt as modifier), you can get × by typing right alt+ “-” and ÷ by right alt + “=”
2 × 3
6
12 ÷ 4
3
As usual.
Let’s see what *
does.
7 * 3
343
As you can see, *
stands for exponentiation.
What if we do operations on arrays?
2 8 + 9
11 17
As you can see, here we added 9
with an array 2 8
When you use + with an array and a number, the number will be added to each element of the array. Similar for other arithmetic operators.
2 8 × 12
24 96
2 8 ÷ 2
1 4
What happens if we operate between two arrays?
12 20 × 2 4
24 80
As you can see, when two arrays are passed to × it multiplies the first elements of both the arrays, the second elements of both the arrays, the third elements and so on.
If the two arrays are not of equal length, you’ll get an error
12 20 × 2 4 5
LENGTH ERROR
12 20×2 4 5
^ ^
One great (or bad) thing about APL is that the symbols change their behaviour based on the context. We’ll come to the monadic, dyadic part later. For now, let’s see what happens if we put the operators before an array —
+ 2 3 4
2 3 4
- 2 3 4
¯2 ¯3 ¯4
× 2 3 4
1 1 1
÷ 2 3 4
0.5 0.3333333333 0.25
Before reading my explanation, try to think for yourself what they did.
As you can see, “+” does nothing. We’ll find where to use it later.
As for “-” you must have noticed, it reverses the sign of every element.
÷ reciprocals the elements.
But what did × do? It seems like it generates an array of 1
. Try this one (press right alt + 2 to get the “high” minus) —
× ¯2 ¯3 0 67
¯1 ¯1 0 1
Get it? It returns 1
for positive elements, 0
for 0
and ¯1 for negative elements. It works as the Signum function.
Finally, let’s try some complex arithmetic —
5×4-2+3
¯5
Wait what? According to BODMAS, shouldn’t it be 15? What a stupid language! Can’t even Math properly!
The fact is, there are no operator precedence in APL, simply because there are so many operators that it’s difficult to remember their precedence. Imagine remembering what occurs before what in a list of 50+ operators.
APL works from right to left. So it first calculates 2+3=5
then 4-5=¯1
and then 5ׯ1
That’ll be enough for the first day. Be sure to practice what you learned from this guide. In the next part I’ll dive deep into the depths of APL, and we’ll see some more operators.
To exit the interpreter type )OFF
at the prompt.