3DSoftware.com > Programming > APL
Introduction to APL
 
APL was a computer programming language in the 1970s. It is still available on some systems. This is an introduction to APL for programmers who have APL available on their system.
 
In our examples, the APL prompt will look like this:
 
 
On your system, the APL prompt may be 6 blank spaces.
 
 
APL starts up in calculator mode (immediate mode).
 
You type a line of APL code after the prompt, and press the Enter/Return key (Carriage Return on line terminals).
 
Then APL displays the result.
 
For example, to multiply 5 by 3, enter 5 × 3 at the APL prompt:
 
 
The result is displayed on the next line. Then the APL prompt appears on the line after that, and waits for you to enter another line of code.
 
Assignment is easy in APL, with the left-arrow symbol.  The following example automatically creates a new variable called A, and assigns the product of 5 × 3 to this new variable:
 
 
As shown, when you enter the name of a variable by itself at the APL prompt, the contents of the variable are displayed.
 
After displaying the contents of the variable in that example, the APL prompt is displayed again, for you to continue using APL. Some of our examples will not show the prompt appearing again for you to continue, but you can assume the APL prompt will continue appearing.
 

 
In the last example above, a variable called A was automatically created. That variable is now part of your APL workspace.
 
To display a list of variables that have already been defined in your workspace, enter )VARS at the prompt. Or to display only the variables that begin with the letter A, enter )VARS A at the prompt.
To undefine a variable you will no longer be using, use the )ERASE command. For example, to eliminate a variable called A1 from your workspace, enter )ERASE A1 at the APL prompt.
 
After you use the )ERASE command to eliminate a variable from your workspace, that variable will no longer be listed by the )VARS command. You can, however, easily create the variable again, with a simple asignment statement.
 

 
Creating vectors in APL is easy. Simply assign a list of numbers to a variable. You can then perform operations on that variable, such as scalar multiplication (as shown in the example at right).
 

 
You can also use a vector on the fly, without assigning it to a variable. And vector components can themselves be expressions.
 

 
APL includes a wealth of functions and adverbs for handling vectors.  An example is “add reduction” which is invoked with +/ (the plus sign followed by the forward slash). That adds the vector components (cells) together. If you use a multiplication sign instead of the plus sign, the components are multiplied together.
 

 
You can also rotate (barrel shift) a vector. That is done with the rotate symbol, which is a combination of the vertical bar and circle, as shown below:
 
 
An important feature of APL is the ability to create such symbols by simply typing one symbol over another (in this case combining the vertical bar symbol with the circle symbol). The number before the rotate symbol specifies how many cells to rotate (toward the left if that number is positive, or toward the right if it is negative).
 

 
The rotate symbol is only a rotate symbol if it has a number before it.
 
If it does not have a number before it, then it is the reversal symbol.
 

 
The last two examples bring up an important point. The same symbol can mean different things depending on whether it has arguments (inputs) on one side or both sides.
 
If a function or symbol has arguments on both sides, it is said to be dyadic.
 
If a function or symbol has input on only one side, it is said to be monadic, and the input will be to the right.
 
Symbols that can be either monadic or dyadic (such as the rotate/reversal symbol) are said to be ambivalent.
 

 
References
 
S. Pommier
An Introduction to APL
Raymond P. Polivka and Sandra Pakin
APL: The Language and Its Usage
 
 
Copyright © 2008 by 3D Software. All rights reserved.
3D Software, P.O. Box 221190, Sacramento CA 95822 USA
www.3DSoftware.com     Contact us
Thursday, 28-Aug-2008 04:11:00 GMT