A Modern Take on an Old Language [Hackaday]
Some old computer languages are destined to never die. They do, however, evolve. For example, Fortran, among the oldest of computer languages, still has adherents, not to mention a ton of legacy code to maintain. But it doesn’t force you to pretend you are using punched cards anymore. In the 1970s, if you wanted to crunch numbers, Fortran was a good choice. But there was another very peculiar language: APL. Turns out, APL is alive and well and has a thriving community that still uses it.
APL has a lot going for it if you are crunching serious numbers. The main data type is a multidimensional array. In fact, you could argue that a lot of “modern” ideas like a REPL, list types, and even functional programming entered the mainstream through APL. But it did have one strange thing that made it difficult to use and learn.
[Kenneth E. Iverson] was at Harvard in 1957 and started working out a mathematical notation for dealing with arrays. By 1960, he’d moved to IBM and a few years later wrote a book entitled “A Programming Language.” That’s where the name comes from — it is actually an acronym for the book’s title. Being a mathematician, [Iverson] used symbols instead of words. For example, to create an array with the numbers 1 to 5 in it and then print it, you’d write:
⎕←⍳5
Since modern APL has a REPL (read-eval-print loop), you could remove the box and the arrow today.
What Key Was That?
Wait. Where are all those keys on your keyboard? Ah, you’ve discovered the one strange thing. In 1963, CRTs were not very common. While punched cards were king, IBM also had a number of Selectric terminals. These were essentially computer-controlled typewriters that had type balls instead of bars that were easy to replace.
With the right type ball, you could have 26 upper-case letters, 10 digits, a few control characters, and then a large number of “weird” characters. But it is actually worse than that. The available symbols were still not numerous enough for APL’s appetite. So some symbols required you to type part of the symbol, press backspace, then type more of the symbols, sometimes repeating the process several times. On a printing terminal, that works fine. For the CRTs that would soon take over, this was tough to do.
For example, a comment (like a REM in Basic or a // in C++) is represented by a thumbnail (⍝). In other words, this would be an APL comment:
⍝ This is a comment
To make that character, you’d type the “arch” part, backspace, then the “dot” part. Not very speedy. Not very practical on old CRT terminals, either.
The characters aren’t the only strange thing. For example, APL evaluates math right to left.
That is, 3×2+5 is 21 because the 2+5 happens first. You just have to get used to that.
A Solution
Of course, modern screens can handle this easily and most people use an APL keyboard mapping that looks like your normal keyboard, but inserts special symbols when you use the right Alt key (with or without the shift modifier). This allows the keyboard to directly enter every possible symbol.
Of course, your keyboard’s keycaps probably don’t have those symbols etched in, so you’ll probably want a cheat sheet. You can buy APL keycaps or even entire keyboards if you really get into it.
What’s GNU With You?
While there have been many versions of APL over the years, GNU APL is certainly the easiest to setup, at least for Linux. According to the website, the project has more than 100,000 lines of C++ code! It also has many modern things like XML parsers.
The real trick is making your keyboard work with the stranger characters. If you are just playing around, you can consider doing nothing. You can see the keyboard layout by issuing the ]KEYBD command at the APL prompt. That will give you something like the adjacent keyboard layout image.
From that image, you can copy and paste odd characters. That’s a pain, though. I had good luck with this command line:
setxkbmap -layout us,apl -variant ,dyalog -option grp:switch
With this setup, I can use the right alt key to get most APL characters. I never figured out how to get the shifted alternate characters, though. If you want to try harder, or if you use a different environment than I do, you might read the APL Wiki.
An Example
Rather than do a full tutorial, here’s my usual binary search high low game. The computer asks you to think of a number, and then it guesses it. Not the best use of APL’s advanced math capabilities, but it will give you an idea of what it can do.
Here’s a survival guide. The upside-down triangle is the start or end of a function. You already know the thumbnail is a comment. A left-pointing arrow is an assignment statement. A right-pointing arrow is a goto (this was created in the 1960s; modern APL has better control structures, but they can vary between implementations). Square boxes are for I/O, and the diamond separates multiple statements on a single line.
∇ BinarySearchGame ⍝ Initialize variables lower ← 1 upper ← 1024 turns ← 0 cheating ← 0 ⍝ Start the game 'Think of a number between 1 and 1024.' ⋄ ⎕ ← '' Loop: turns ← turns + 1 guess ← ⌊(lower + upper) ÷ 2 ⍝ Make a guess using binary search ⍞ ← 'Is your number ', ⍕ guess, '? (h for high, l for low, c for correct): ' response ← ⍞ → (response = 'c')/Finish ⍝ Jump to Finish if correct → (response = 'h')/TooHigh ⍝ Jump to TooHigh if too high → (response = 'l')/TooLow ⍝ Jump to TooLow if too low → InvalidInput ⍝ Invalid input TooHigh: upper ← guess - 1 → (lower > upper)/CheatingDetected ⍝ Detect cheating → Loop TooLow: lower ← guess + 1 → (lower > upper)/CheatingDetected ⍝ Detect cheating → Loop InvalidInput: ⍞ ← 'Invalid input. Please enter "h", "l", or "c".' ⋄ ⎕ ← '' turns ← turns - 1 ⍝ Invalid input doesn't count as a turn → Loop CheatingDetected: ⍞ ← 'Hmm... Something doesn''t add up. Did you make a mistake?' ⋄ ⎕ ← '' cheating ← 1 → Finish Finish: → (cheating = 0)/Continue ⍝ If no cheating, continue → EndGame Continue: ⍞ ← 'Great! The number is ', ⍕ guess, '. It took ', ⍕ turns, ' turns to guess it.' ⋄ ⎕ ← '' EndGame: ⍞ ← 'Would you like to play again? (y/n): ' restart ← ⍞ → (restart = 'y')/Restart ⍝ Restart the game if 'y' → Exit ⍝ Exit the game otherwise Restart: BinarySearchGame ⍝ Restart the game Exit: ⍞ ← 'Thank you for playing!' ⋄ ⎕ ← '' ⍝ Exit message ∇
What’s Next?
If you want to get an idea of how APL’s special handling of data make some programs easier, the APL Wiki has a good page for that. If you don’t want to install anything, you can run APL in your browser (although it is the Dyalog version, a very common choice for modern APL).
If you don’t want to read the documentation, check out [phoebe’s] video below. We always wanted the IBM computer that had the big switch to go from Basic to APL.
APL Keyboard image via Reddit