Dyalog APL, Version 11

Share This Post

Dyalog APL, Version 11

Dylog Limited

http://www.dyalog.com/ +44(0)1256 338461

In his wayward youth this editor was an APL programmer. At that time, before even large companies could afford their own computers, APL was the leading programming language. Indeed, the first portable-ish computer from IBM had APL built into it.

APL had one big virtue: It was very fast to program because it uses less code than do other computer languages. As an example of the small amount of code involved, imagine you are an average programmer, and your boss asks you to write a program to do simple arithmetic averages. How do you calculate an average? This isn’t a trick question: You add up the numbers and divide by the number of numbers.

In APL operations can be performed across a bunch of numbers using the forward slash symbol (/) which means that if you wanted to add up the numbers 1 2 3 you would type in

+/1 2 3

hit return, and the computer would print the number 6. To show the number of numbers, APL uses the Greek letter rho (ρ). In theory, then, to get the average of 1 2 3 you would type in

+/1 2 3÷ρ1 2 3

and the computer would return 2.

In practice this is not the best way to do it, as APL has no rules of precedence: Instead, it works strictly left to right, so, if written as above, it would add up each of the individual numbers divided by the number of numbers. That is to say that it would add up 1÷3 and 2÷3 and 3÷3. While this is not a big deal with three numbers, it is a lot of extra processing with ten thousand numbers. To get around this we put parentheses around the numbers to be added up, which gives us

(+/1 2 3)÷ρ1 2 3

which does one addition and one division.

We obviously don’t want to keep typing individual number, so we use variables, which are assigned using a left pointing arrow. So now our computation would look be

nums←1 2 3

(+/nums)÷ρnums

The only problem with this is that APL works on multi-dimensional arrays, and the / really works on the first dimension. Since we want the average of all the numbers, independent of the shape of the array, we use a comma to ravel an array to a vector. Which means or calculation now becomes

nums←,1 2 3

(+/nums)÷ρnums

For readability we would probably use a separator character, which is a diamond, and assign the number and do the calculation on a single line

nums←,1 2 3 ◊ (+/nums)÷ρnums

So how do we turn this into a program that will take in a bunch of numbers and pass a result that can be used by other programs? The del character tells the computer that we are going into self-improvement mode and creating a new function. So our almost-complete program might say

∇ r←avg nums

[1] nums←,nums ◊ (+/nums)÷ρnums

The variable r was chosen arbitrarily. Why is the program only almost complete?

Because it is imprudent to write any program that lacks comments telling what it is you are doing. Or at least think you are doing. APL uses a little filament (as if from a lightbulb) before the comment to allow you to illuminate your code. So our real program might (at the very least) say

∇ r←avg nums

[1] ⍝ Purpose: Simple arithmetic average, shape independent.

[2] nums←,nums ◊ (+/nums)÷ρnums ⍝ Ravel numbers to a vector, add them up, and divide by the number of numbers.

Congratulations: You are now an APL programmer!

How would this compare with a program in other languages? Ask someone on your programming staff to show you the code for the program they have written to do simple arithmetic averages: You will be horrified.

As it turns out, APL is not only fast to write, but it also runs fast. It was not uncommon to see someone develop a prototype in APL in a short period of time, then spend a huge amount of time and money converting it to what they thought of as a real programming language, only to find that the APL prototype ran almost as fast, and sometimes faster. And the application could be maintained more easily in APL because there was so much less code. APL largely disappeared for three reasons. First, by the beginning of the 1980s corporations were able to afford computers. They wanted to buy the commercial version of APL that they had been using for timesharing, but the timesharing companies, not recognizing that the paradigm had shifted, didn’t want to sell it. By the time they came to their senses, their clients had largely gone in-house with other languages.

Second, when the young Microsoft tried to buy an APL interpreter, the timesharing vendors wouldn’t sell it, so Microsoft chose Basic as their programming platform.

Third, while APL’s development efficiency was good for a timesharing environment, it was a disadvantage in the corporate environment. As the VP of a large insurance company put it, “I have 600 COBOL programmers working for me, which makes me a very senior member of the management team. If I did this work in APL I’d have 40 people working for me and be a division leader.” Since managers are often rewarded on headcount and budget managed, APL was an inappropriate career move for a cost center.

After assuming that APL had been pretty much dead, out of the blue this editor mysteriously received three inquiries about doing APL programming work. This lead us to believe it was time to look at APL in its current incarnation, and see how it had changed in the last decade or so.

We looked at Dylog APL, and were very impressed: This is definitely not your mother’s APL!

All the fundamentals were there, and worked as flawlessly and efficiently as you would expect from an industrial-strength programming language. A lot had been added that was not in APL when we were programming. We will ignore here new primitive functions (the single symbols) because we think they will be of more interest to programmers than to managers. What is of interest is that APL now includes GUI facilities that did not exist in our past, tools to integrate to anything else on your network, and even a .Net interface. What does this mean in practical terms? It means that you can have access to all the data in a modern network environment and slash your programming effort.

While we frankly don’t envision APL regaining the position it once had – it still faces the defect of needing far fewer programmers than if you work in Basic, C, COBOL, or any other programming language – it remains a viable, state of the art, and effective programming environment.

If you find yourself having computer project responsibility, but not enough time or money, Dyalog APL is well worthy of your consideration.

More To Explore