APL syntax and symbols
The programming language APL is distinctive in being symbolic rather than lexical: its primitives are denoted by symbols, not words. These symbols were originally devised as a mathematical notation to describe algorithms. APL programmers often assign informal names when discussing functions and operators but the core functions and operators provided by the language are denoted by non-textual symbols.
Monadic and dyadic functions
Most symbols denote functions or operators. A monadic function takes as its argument the result of evaluating everything to its right. A dyadic function has another argument, the first item of data on its left. Many symbols denote both monadic and dyadic functions, interpreted according to use. For example, ⌊3.2 gives 3, the largest integer not above the argument, and 3⌊2 gives 2, the lower of the two arguments.Functions and operators
APL uses the term operator in Heaviside’s sense as a moderator of a function as opposed to some other programming language's use of the same term as something that operates on data, ref. relational operator and operators generally. Other programming languages also sometimes use this term interchangeably with function, however both terms are used in APL more precisely. Early definitions of APL symbols were very specific about how symbols were categorized, ref. IBM's 5100 APL Reference Manual, first edition, circa 1975. For example, the operator reduce is denoted by a forward slash and reduces an array along one axis by interposing its function operand. An example of reduce:In the above case, the reduce or slash operator moderates the multiply function. The expression ×/2 3 4 evaluates to a scalar result through reducing an array by multiplication. The above case is simplified, imagine multiplying more than just a few numbers together.
The above dyadic functions examples demonstrate how boolean values can be used as left arguments for the \ expand and / replicate functions to produce exactly opposite results. On the left side, the 2-element vector is expanded where boolean 0s occur to result in a 3-element vector - note how APL inserted a 0 into the vector. Conversely, the exact opposite occurs on the right side - where a 3-element vector becomes just 2-elements; boolean 0s delete items using the dyadic / slash function. APL symbols also operate on lists of items using data types other than just numeric, for example a 2-element vector of character strings could be substituted for numeric vector above.
Syntax rules
In APL there is no precedence hierarchy for functions or operators. APL does not follow the usual operator precedence of other programming languages; for example,×
does not bind its operands any more "tightly" than +
. Instead of operator precedence, APL defines a notion of scope.The scope of a function determines its arguments. Functions have long right scope: that is, they take as right arguments everything to their right. A dyadic function has short left scope: it takes as its left arguments the first piece of data to its left. For example, :
1 ÷ 2 ⌊ 3 × 4 - 5
¯0.3333333333
1 ÷ 2 ⌊ 3 × ¯1
¯0.3333333333
1 ÷ 2 ⌊ ¯3
¯0.3333333333
1 ÷ ¯3
¯0.3333333333
<< First note there are no parentheses and
APL is going to execute from right-to-left.
Step 1) 4-5 = -1.
Step 2) 3 times -1 = -3.
Step 3) Take the floor or lower of 2 and -3 = -3.
Step 4) Divide 1 by -3 = -0.3333333333 = final result.
An operator may have function or data operands and evaluate to a dyadic or monadic function. Operators have long left scope. An operator takes as its left operand the longest function to its left. For example:
∘.=/⍳¨3 3
1 0 0
0 1 0
0 0 1
APL atomic or piecemeal sub-analysis :
Beginning rightmost: ⍳¨3 3 produces a 2-element nested APL vector where each element is itself a vector. Iota ⍳3 by itself would produce.
The diaeresis ¨ or mini double-dot means repeat or over each or perform each separately so iota repeats, concisely: iota for each 3.
The left operand for the over-each operator
¨
is the index ⍳ function. The derived function ⍳¨
is used monadically and takes as its right operand the vector 3 3
. The left scope of each is terminated by the reduce operator, denoted by the forward slash. Its left operand is the function expression to its left: the outer product of the equals function. The result of ∘.=/ is a monadic function. With a function's usual long right scope, it takes as its right argument the result of ⍳¨3 3. Thus'
1 2 3 1 2 3
∘.=⍳3
1 0 0
0 1 0
0 0 1
1 2 3 1 2 3
∘.=/⍳¨3 3
1 0 0
0 1 0
0 0 1
Equivalent results in APL: ' and << Rightmost expression is.
The matrix of 1s and 0s similarly produced by ∘.=/⍳¨3 3 and ∘.=⍳3 is called an identity matrix.
Identity matrices are useful in solving matrix determinants, groups of linear equations and multiple regression.
im ← ∘.=⍨∘⍳
im 3
1 0 0
0 1 0
0 0 1
Some APL interpreters support the compose operator ∘ and the commute operator ⍨. The former ∘ glues functions together so that foo∘bar, for example, could be a hypothetical function that applies defined function foo to the result of defined function bar; foo and bar can represent any existing function. In cases where a dyadic function is moderated by commute and then used monadically, its right argument is taken as its left argument as well. Thus, a derived or composed function is used in the APL user session to return a 9-element identity matrix using its right argument, parameter or operand = 3.
Letters←"ABCDE"
Letters
ABCDE
⍴Letters
FindIt←"CABS"
FindIt
CABS
⍴FindIt
Letters ⍳ FindIt
3 1 2 6
Example using APL to index ⍳ or find elements in a character vector:
First, variable Letters is assigned a vector of 5-elements, in this case - letters of the alphabet.
The shape ⍴ or character vector-length of Letters is 5.
Variable FindIt is assigned what to search for in Letters and its length is 4 characters.
1 2 3 4 5 << vector positions or index #'s in Letters
ABCDE
At left, dyadic function iota searches through its left argument for the search string.
Iota finds letter "C" at position 3 in Letters, it finds "A" at position 1, and "B" at position 2. Iota does not find letter "S"
anywhere in variable Letters so it returns the number 6 which is 1 greater than the length of Letters. Iota found letters
"CAB". Iota correctly did not find "S".
Monadic functions
Dyadic functions
Operators and axis indicator
Notes: The reduce and scan operators expect a dyadic function on their left, forming a monadic composite function applied to the vector on its right.The product operator "." expects a dyadic function on both its left and right, forming a dyadic composite function applied to the vectors on its left and right. If the function to the left of the dot is "∘" then the composite function is an outer product, otherwise it is an inner product. An inner product intended for conventional matrix multiplication uses the + and × functions, replacing these with other dyadic functions can result in useful alternative operations.
Some functions can be followed by an axis indicator in brackets, i.e., this appears between a function and an array and should not be confused with array subscripts written after an array. For example, given the ⌽ function and a two-dimensional array, the function by default operates along the last axis but this can be changed using an axis indicator:
A←4 3⍴⍳12
A
1 2 3
4 5 6
7 8 9
10 11 12
⌽A
3 2 1
6 5 4
9 8 7
12 11 10
⌽A
10 11 12
7 8 9
4 5 6
1 2 3
⊖⌽A
12 11 10
9 8 7
6 5 4
3 2 1
⍉A
1 4 7 10
2 5 8 11
3 6 9 12
4 rows by 3 cols matrix created, using rho ⍴ and iota ⍳. The 4 x 3 matrix is then stored in a variable named A.
A is now reflected or flipped along its vertical axis as symbol ⌽ visually indicates.
A is now reflected using the axis indicator or first dimension modifier. The result is that variable A has been reflected across the horizontal axis, instead of vertically.
A is now reflected both vertically ⊖ and horizontally ⌽.
A is ⍉ transposed to a 3 row by 4 col matrix such that rows-cols become exchanged, as symbol ⍉ visually portrays. Compare the result here to the original matrix stored in A, topmost matrix. These types of data transformations are useful in time series analysis and spatial coordinates, just two examples, more exist.
As a particular case, if the dyadic catenate "," function is followed by an axis indicator, it can be used to laminate two arrays depending on whether the axis indicator is less than or greater than the index origin :
B←1 2 3 4
C←5 6 7 8
B,C
1 2 3 4 5 6 7 8
B,C
1 2 3 4
5 6 7 8
B,C
1 5
2 6
3 7
4 8
At left, variable 'B' is first assigned a vector of 4 consecutive integers.
Var C is then assigned 4 more consecutive integers.
'B' and C are then concatenated or raveled together for illustration purposes,
resulting in a single vector.
In the particular case at left, if the dyadic catenate "," function is followed by an axis indicator, it can be used to laminate two arrays depending on whether the axis indicator is less than or greater than the index origin. The first result is a 2 row by 4 column matrix, vertically joining 'B' and C row-wise. The second result is a 4 row by 2 column matrix.
Nested arrays
Arrays are structures which have elements grouped linearly as vectors or in table form as matrices - and higher dimensions. Arrays containing both characters and numbers are termed mixed arrays. Array structures containing elements which are also arrays are called nested arrays.Explanation | |
X←4 5⍴⍳20 X 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 X ⎕IO X | X set = to matrix with 4 rows by 5 columns, consisting of 20 consecutive integers. Element X in row 2 - column 2 currently is an integer = 7. Initial index origin ⎕IO value = 1. Thus, the first element in matrix X or X = 1. |
X←⊂"Text" X←⊂ X 1 2 3 4 5 6 Text 8 9 10 11 12 13 1 2 15 3 4 16 17 18 19 20 | Element in X is changed to a nested vector "Text" using the enclose ⊂ function. Element in X, formerly integer 14, now becomes a mini enclosed or ⊂ nested 2x2 matrix of 4 consecutive integers. Since X contains numbers, text and nested elements, it is both a mixed and a nested array. |
Flow control
A user may define custom functions which, like variables, are identified by name rather than by a non-textual symbol. The function header defines whether a custom function is niladic, monadic or dyadic, the local name of the result, and whether it has any local variables.Niladic function PI or π | ||
∇ RESULT←PI RESULT←○1 ∇ | ∇ AREA←CIRCLEAREA RADIUS AREA←PI×RADIUS⋆2 ∇ | ∇ AREA←DEGREES SEGMENTAREA RADIUS ; FRACTION ; CA FRACTION←DEGREES÷360 CA←CIRCLEAREA RADIUS AREA←FRACTION×CA ∇ |
Whether functions with the same identifier but different adicity are distinct is implementation-defined. If allowed, then a function CURVEAREA could be defined twice to replace both monadic CIRCLEAREA and dyadic SEGMENTAREA above, with the monadic or dyadic function being selected by the context in which it was referenced.
Custom dyadic functions may usually be applied to parameters with the same conventions as built-in functions, i.e., arrays should either have the same number of elements or one of them should have a single element which is extended. There are exceptions to this, for example a function to convert pre-decimal UK currency to dollars would expect to take a parameter with precisely three elements representing pounds, shillings and pence.
Inside a program or a custom function, control may be conditionally transferred to a statement identified by a line number or explicit label; if the target is 0 this terminates the program or returns to a function's caller. The most common form uses the APL compression function, as in the template /target which has the effect of evaluating the condition to 0 or 1 and then using that to mask the target.
Hence function SEGMENTAREA may be modified to abort, returning zero if the parameters are of different sign:
∇ AREA←DEGREES SEGMENTAREA RADIUS ; FRACTION ; CA ; SIGN ⍝ local variables denoted by semicolon
FRACTION←DEGREES÷360
CA←CIRCLEAREA RADIUS ⍝ this APL code statement calls user function CIRCLEAREA, defined up above.
SIGN←≠×RADIUS ⍝ << APL logic TEST/determine whether DEGREES and RADIUS do NOT have same SIGN 1-yes different, 0-no
AREA←0 ⍝ default value of AREA set = zero
→SIGN/0 ⍝ branching occurs when SIGN=1 while SIGN=0 does NOT branch to 0. Branching to 0 exits function.
AREA←FRACTION×CA
The above function SEGMENTAREA works as expected if the parameters are scalars or single-element arrays, but not if they are multiple-element arrays since the condition ends up being based on a single element of the SIGN array - on the other hand, the user function could be modified to correctly handle vectorized arguments. Operation can sometimes be unpredictable since APL defines that computers with vector-processing capabilities should parallelise and may reorder array operations as far as possible - thus, test and debug user functions particularly if they will be used with vector or even matrix arguments. This affects not only explicit application of a custom function to arrays, but also its use anywhere that a dyadic function may reasonably be used such as in generation of a table of results:
90 180 270 ¯90 ∘.SEGMENTAREA 1 ¯2 4
0 0 0
0 0 0
0 0 0
0 0 0
A more concise way and sometimes better way - to formulate a function is to avoid explicit transfers of control, instead using expressions which evaluate correctly in all or the expected conditions. Sometimes it is correct to let a function fail when one or both input arguments are incorrect - precisely to let user know that one or both arguments used were incorrect. The following is more concise than the above SEGMENTAREA function. The below importantly correctly handles vectorized arguments:
∇ AREA←DEGREES SEGMENTAREA RADIUS ; FRACTION ; CA ; SIGN
FRACTION←DEGREES÷360
CA←CIRCLEAREA RADIUS
SIGN←≠×RADIUS
AREA←FRACTION×CA×~SIGN ⍝ this APL statement is more complex, as a one-liner - but it solves vectorized arguments: a tradeoff - complexity vs. branching
∇
90 180 270 ¯90 ∘.SEGMENTAREA 1 ¯2 4
0.785398163 0 12.5663706
1.57079633 0 25.1327412
2.35619449 0 37.6991118
0 ¯3.14159265 0
Avoiding explicit transfers of control also called branching, if not reviewed or carefully controlled - can promote use of excessively complex one liners, veritably "misunderstood and complex idioms" and a "write-only" style, which has done little to endear APL to influential commentators such as Edsger Dijkstra. Conversely however APL idioms can be fun, educational and useful - if used with helpful comments ⍝, for example including source and intended meaning and function of the idiom. Here is an , an and .
Miscellaneous
Most APL implementations support a number of system variables and functions, usually preceded by the ⎕ " character. Particularly important and widely implemented is the ⎕IO variable, since while the original IBM APL based its arrays on 1 some newer variants base them on zero:Description | |
X←⍳12 X 1 2 3 4 5 6 7 8 9 10 11 12 ⎕IO X | X set = to vector of 12 consecutive integers. Initial index origin ⎕IO value = 1. Thus, the first position in vector X or X = 1 per vector of iota values. |
⎕IO←0 X X | Index Origin ⎕IO now changed to 0. Thus, the 'first index position' in vector X changes from 1 to 0. Consequently, X then references or points to 2 from and X now references 1. |
⎕WA 41226371072 | Quad WA or ⎕WA, another dynamic system variable, shows how much Work Area remains unused or 41,226 megabytes or about 41 gigabytes of unused additional total free work area available for the APL workspace and program to process using. If this number gets low or approaches zero - the computer may need more random-access memory, hard disk drive space or some combination of the two to increase virtual memory. |
)VARS | )VARS a system function in APL, )VARS shows user variable names existing in the current workspace. |
There are also system functions available to users for saving the current workspace e.g., )SAVE and terminating the APL environment, e.g., )OFF - sometimes called hook commands or functions due to the use of a leading right parenthesis or hook. There is some standardization of these quad and hook functions.
Fonts
The Unicode Basic Multilingual Plane includes the APL symbols in the Miscellaneous Technical block, which are thus usually rendered accurately from the larger Unicode fonts installed with most modern operating systems. These fonts are rarely designed by typographers familiar with APL glyphs. So, while accurate, the glyphs may look unfamiliar to APL programmers or be difficult to distinguish from one another.Some Unicode fonts have been designed to display APL well: APLX Upright, APL385 Unicode, and SimPL.
Before Unicode, APL interpreters were supplied with fonts in which APL characters were mapped to less commonly used positions in the ASCII character sets, usually in the upper 128 code points. These mappings were sometimes unique to each APL vendor's interpreter, which made the display of APL programs on the Web, in text files and manuals - frequently problematic.
APL2 keyboard function to symbol mapping
Note the APL On/Off Key - topmost-rightmost key, just below. Also note the keyboard had some 55 unique APL symbol keys, thus with the use of alt, shift and ctrl keys - it would theoretically have allowed a maximum of some 59 *4 *3 or some 472 different maximum key combinations, approaching the 512 EBCDIC character max. Again, in theory the keyboard pictured below would have allowed for about 472 different APL symbols/functions to be keyboard-input, actively used. In practice, early versions were only using something roughly equivalent to 55 APL special symbols. Thus, early APL was then only using about 11% of a symbolic language's at-that-time utilization potential, based on keyboard # keys limits, again excluding numbers, letters, punctuation, etc. In another sense keyboard symbols utilization was closer to 100%, highly efficient, since EBCDIC only allowed 256 distinct chars, and ASCII only 128.Solving puzzles
APL has proved to be extremely useful in solving mathematical puzzles, several of which are described below.Pascal's triangle
Take Pascal's triangle, which is a triangular array of numbers in which those at the ends of the rows are 1 and each of the other numbers is the sum of the nearest two numbers in the row just above it. The following is an APL one-liner function to visually depict Pascal's triangle:Pascal← ⍝ Create one-line user function called Pascal
Pascal 7 ⍝ Run function Pascal for seven rows and show the results below:
1
1 2
1 3 3
1 4 6 4
1 5 10 10 5
1 6 15 20 15 6
1 7 21 35 35 21 7
Prime numbers, contra proof via factors
Determine the number of prime numbers up to some number N. Ken Iverson is credited with the following one-liner APL solution to the problem:⎕CR 'PrimeNumbers' ⍝ Show APL user-function PrimeNumbers
Primes←PrimeNumbers N ⍝ Function takes one right arg N
Primes←/⍳N ⍝ The Ken Iverson one-liner
PrimeNumbers 100 ⍝ Show all prime numbers from 1 to 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
⍴PrimeNumbers 100
25 ⍝ There are twenty-five prime numbers in the range up to 100.
Examining the converse or opposite of a mathematical solution is frequently needed : Prove for the subset of integers from 1 through 15 that they are non-prime by listing their decomposition factors. What are their non-one factors ?
⎕CR 'ProveNonPrime'
Z←ProveNonPrime R
⍝Show all factors of an integer R - except 1 and the number itself,
⍝ i.e., prove Non-Prime. String 'prime' is returned for a Prime integer.
Z←/⍳R ⍝ Determine all factors for integer R, store into Z
Z←/Z ⍝ Delete 1 and the number as factors for the number from Z.
→/ProveNonPrimeIsPrime ⍝ If result has zero shape, it has no other factors and is therefore prime
Z←R,,,⎕TCNL ⍝ Show the number R, its factors, and a new line char
→0 ⍝ Done with function if non-prime
ProveNonPrimeIsPrime: Z←R,,⎕TCNL ⍝ function branches here if number was prime
ProveNonPrime ¨⍳15 ⍝ Prove non primes for each of the integers from 1 through 15
1 prime
2 prime
3 prime
4 factors 2
5 prime
6 factors 2 3
7 prime
8 factors 2 4
9 factors 3
10 factors 2 5
11 prime
12 factors 2 3 4 6
13 prime
14 factors 2 7
15 factors 3 5
Fibonacci sequence
Generate a Fibonacci number sequence, where each subsequent number in the sequence is the sum of the prior two:⎕CR 'Fibonacci' ⍝ Display function Fibonacci
FibonacciNum←Fibonacci Nth;IOwas ⍝ Funct header, funct name=Fibonacci, monadic funct with 1 right hand arg Nth;local var IOwas, and a returned num.
⍝Generate a Fibonacci sequenced number where Nth is the position # of the Fibonacci number in the sequence. << function description
IOwas←⎕IO ⋄ ⎕IO←0 ⋄ FibonacciNum←↑0 1↓↑+.×/Nth/⊂2 2⍴1 1 1 0 ⋄ ⎕IO←IOwas ⍝ In order for this function to work correctly ⎕IO must be set to zero.
Fibonacci¨⍳14 ⍝ This APL statement says: Generate the Fibonacci sequence over each integer number for the integers 1..14.
0 1 1 2 3 5 8 13 21 34 55 89 144 233 ⍝ Generated sequence, i.e., the Fibonacci sequence of numbers generated by APL's interpreter.
Generic online tutorials
- by Graeme Donald Robertson
- full-featured, no restrictions, free downloadable APL/2 with nested arrays by Sudley Place Software
- free downloadable interpreter for APL by Jürgen Sauermann
- uploaded by Jimin Park, 8 intro/beginner instructional videos.
- by MicroAPL
Syntax rules