[Next] [Up] [Previous] [Next Section] [Home] [Other]

Remembering APL

APL, which stands for A Programming Language, had its roots in a notation for algorithms devised by Kenneth E. Iverson. This notation, however, differed in several important respects from the APL computer language we are familiar with today. All the implementations of APL of which I am aware derive from the version of the language used in the IBM program product APL/360.

One of the most distinctive features of APL is that it uses a special character set. Thus, an APL keyboard may look like this:

This is a 44-key typewriter keyboard. The terminal originally used with APL was an IBM 2741, which was a KSR (keyboard send-receive) printing terminal based on the IBM Selectric typewriter. The printing element on this terminal could, therefore, be freely interchanged, and it was the famous "golfball" element with 88 characters.

For use with APL, stickers were applied to the front of the keys.

There were numerous other devices with APL keyboards of one type or another. The IBM 5100 portable computer offered APL as one of the two languages available for it, and thus had an APL keyboard:

IBM also offered a version of their 3277 terminal which had an APL character set; this was actually the least expensive way to get a 3277 which had lower-case support as well.

APL/360 provided timesharing capability to IBM System/360 computers without requiring them to have Dynamic Address Translation hardware, as was only offered on the Model 67, or a time-sharing operating system such as TSS or TSO running on OS/360. This was possible as users could only run interpreted programs in the APL language, not programs written in assembly language, or programs written in FORTRAN and compiled.

Thus, the APL/360 program was an applications program that was connected to a number of terminals, and which shared time between them in software. This method of providing time-sharing in an interpretive language environment without the need for a full time-sharing operating system had been used before, particularly with the computer language BASIC, but also with modified versions of FORTRAN.

APL is also not the first computer language to attempt to provide an approach to more natural mathematical notation through the use of a special character set, or even the first language to use overstruck characters. The language COLASL, used on the STRETCH computer at Los Alamos, used a special terminal device which allowed the use of a large character set. The letters of the alphabet could be overstruck with squares, circles, or several other symbols to increase the number of available variables.

Originally, it was envisaged that, in addition to using the underline character to increase the number of letters for forming variable names, that the box character could enclose a letter, and the diaresis (two dots) could appear above the letter. Since in APL, as in most other conventional computer languages, a variable name could be a word of several letters, not just a single letter (following mathematical notation, as done in languages such as COLASL, MADCAP, MIRFAC, and the Klerer-May System) presumably no need for this was seen.

Interestingly enough, one way in which COLASL could have influenced APL, as, while the language itself was developed at Los Alamos, IBM provided them with the 9210 Scientific Descriptive Printer, used to prepare punched cards for the input of COLASL programs, is that in the printed form of a COLASL program, the digits from 0 to 9 were, to make them more distinctive from letters (both upper and lower-case were available for preparing COLASL programs) of a reduced height, and italicized. This could have helped to inspire the use of italic letters with APL/360 later.

Incidentally, the arrangement used for the APL keyboard resembled that used for CALL/360 BASIC:

The degree symbol was the shift of J, where the null symbol was in APL; the equals sign, used for assignment in BASIC, was where the left arrow was in APL, and the parentheses were on the same keys as in APL, but as unshifted characters. As well, the up-arrow, used for exponentiation in BASIC, was on the same key as the power symbol in APL, and the symbols used for the four basic arithmetic operators were in the same place as well.

However, APL/360 predated CALL/360 BASIC; as the CALL/360 service also offered APL, it was no doubt decided to use a keyboard arrangement that would reduce confusion for customers using both languages.

But what was unique about APL was that it not only allowed arrays and matrices to be directly manipulated, but that it provided a very powerful set of primitive operations for this.

Let us now begin an introduction to APL.

On the left, we see a transcript of a small portion of a hypothetical APL terminal session. For clarity, what is typed by the user is in red, and what is typed by the computer is in blue.

In APL, the interpreter doesn't print a prompt character on the paper to indicate that it is requesting input, it simply indents six spaces. If an expression is typed, the interpreter responds by printing its value.

Our first example input illustrates addition. The user asks for 4 plus 3, and the computer correctly replies with 7.

Our second example illustrates a subtraction calculation which produces a negative number as a result. Why does (or, rather, why did the earliest implementations of) APL use italic letters, instead of something more natural and useful? The answer was so that it would be easy, by sight, to tell the letter O from the numeral 0. Unlike some old typewriter keyboards, computer keyboards always distinguish the numeral 1 from the (lowercase) letter l and the (uppercase) letter I. In APL, there is also a different character for the minus sign and the subtraction operator.

Our third example does nothing, it appears. But that is because the arrow pointing left is an assignment operator. A value is being stored in the variable A. That lets us use A as an element in subsequent calculations.

But, wait a moment. What kind of an expression is three numbers, with spaces between them?

The answer is that it is a vector-valued constant. As we see in the next line. We multiply A by two, and the answer is a row of three numbers, each one twice the corresponding number in A. This illustrates one of the important aspects of the power of APL. A variable can contain an array of numbers, and operations act on entire arrays, not just on individual values.

Let us continue.

Since the power of APL lies largely in its ability to handle vectors automatically, many of our examples will use vectors. So, one of the first functions we will look at in APL that is related to vectors is one that lets us create vectors without typing out each element in a vector.

Thus, we see that the Greek letter iota is used as an operator which, when applied to n, creates the first n integers in order.

We assign a vector of the numbers from 1 to 6 to V, and then check that V is what we expected.

Our fourth example command uses another Greek letter, the letter rho. This is the restructure function, and turns our 6-element vector into a 2 by 3 matrix. After seeing this in action, we assign this matrix to M.

Now we see what the forward slash is used for in APL. The traditional division symbol is used for division; the forward slash is used for a powerful function that builds a new function from an operator. The sum of the elements of V is what it produces in this example, which is 21.

When we apply it to a matrix, the result will be a vector, the operation being performed over the last dimension of the matrix.

Our final example is a simpler one; we use the relational and logical operators on our vector V to produce a vector containing zeroes except when the corresponding element of V is both greater than 2 and less than 6. Note the placement of parentheses; this illustrates that in APL, the order of evaluation is strictly from right to left. A simple rule such as that is needed in a language like APL with a large number of operators, and this particular rule works best with a conventional right-to-left assignment operator.

Of course, this language is a programming language. Thus, the next thing we should see is how to write programs in it.

By starting a line with the upside-down triangle symbol, we begin a function definition. Until the end of the function definition is signalled by another such symbol, we will be prompted with a line number from now on.

The function header begins the function. Here, the function returns no result, and is a unary operator, having only one argument on the right.

The semicolons after the function prototype separate the local variables to be used within the function.

This function solves the quadratic equation. The three coefficients in the polynomial are given in a vector, so we begin by removing these components from the vector, and assigning them to three simple values.

Then, we calculate the discriminant, b squared minus 4ac.

Statement 5 is a three-way branch. The unary multiply is the signum function, and so it returns -1, 0, or 1, depending on whether the discriminant is negative, zero, or positive. Statement labels are the numbers of the lines they are on; the comma concatenates them into an array, and two plus the signum forms the index into the array. The result, the line to branch to, is given to the right arrow, which is the APL branch operator.

Statement 6 is a comment line. Its first character is not found on the APL keyboard, because it is an overstrike, formed by backspacing so that the intersection and null characters are printed on top of each other.

Statement 7 begins with a statement label. NEG is a variable containing the value 7; if the function is edited, NEG will contain the number of whatever line it is on, facilitating its use as a branch target. A string constant is what the line consits of, and thus, that value will be printed when the line is executed; thus, the program announces that we have two complex roots when the discriminant is negative.

In statement 9, we meet the star, which, in APL, is an exponentiation operator.

In statement 10, we see that the semicolon is used to allow printing numbers and character strings on the same line.

Line 12 is a branch to statement 0; this is a program exit.

It may be noted that this example program could be cited almost as an example of how not to program in APL. The fact that it uses branching, as well as the way it treats its input argument, limits it to arguments in only one form, a three-element vector. Thus, the power of APL arguments to act on arrays of any size is not used by this function. For some problems, that is necessary. For others, branching would still be used, but at a high level of the program, with flexible array operations still taking place within it.

Only a small amount of APL has been illustrated here. Hopefully, it is enough to give a brief idea of the language; there are many resources on APL available on the web.

However, this site continues on for those who would wish to delve more deeply into the subject before leaving this site.

[Next] [Up] [Previous] [Next Section] [Home] [Other]