APL Expressions#

APL is a mathematical notation which just happens to be machine executable, i.e. be A Programming Language. APL expressions consist of functions, arrays and assignments. They may also have trailing comments, beginning with the lamp symbol (a glowing bulb filament to enlighten you).

⍝ This is a comment - nothing happens

You can have multiple expressions on a single line by separating them with a diamond:

2+2  3+3
4 6

Functions#

We call all code that can be applied to data functions, even primitive built-in things. So + and f in f(x) are both functions. (APL operators are something else.)

+
+
5-2
3

Many APL functions can be applied as prefix (monadically) or infix (dyadically). Usually, the monadic and dyadic forms are closely related. E.g. -b is 0-b:

-3   ⍝ APL uses high minus to indicate negative numbers
¯3

Arrays#

All data resides in arrays. An array is a rectangular collection of numbers, characters and arrays, arranged along zero or more axes. We can use more specific terms for some arrays, like a single number is a scalar, a list is a vector, and 2D arrays are matrices. Vectors can be formed by just placing elements next to each other:

1 2 3
1 2 3

Strings#

As APL has no string data type, we just use character vectors (lists) instead. Characters and character vectors are in single quotes:

'a'
a
'Hello, World!'   ⍝ a famous program written in APL!
Hello, World!

Numbers#

A number is a number is a number: You don’t have to deal with internal representation as APL will internally convert between bit-Booleans, integers (of various sizes), floats, base-10 decimal floats, and complex numbers — all seamless to you:

5>4
1
2+(5>4)
3
2÷3
0.666667
1+(2÷3)
1.66667

APL takes care of binary-vs-decimal inexactness too, so 3×(1÷3) is really 1:

3×(1÷3)
1

Negative numbers are indicated by a high minus:

3-5
¯2

Very large or small numbers use exponential notation, separating the significand from the mantissa with an E. In other words, an E embedded in a number reads as times-ten-to-the-power-of:

1000×1000×1000×1000
1E12
1e¯6
0.000001

Complex numbers are similar in that they use a J to separate the real and imaginary parts:

0j1×0j2
¯2
1j2×2j1
0J5

Assignments#

You can assign a function or an array to a name using :

plus+
two2
three3
two plus three
5

This avoids all confusion of assignment with equality. = is just another comparison function:

two=three
0
two=2
1

Why use APL instead of conventional mathematical notation?#

Mathematics has some inconsistent and weird notations, like negation and factorial being on opposite sides of their argument, exponentiation doesn’t have a symbol (rather the exponent is raised and made smaller) and magnitude has two symbols (a vertical bar on each side). APL harmonises everything to be in-line pre/infix single-symbols, so factorial is !b, raising to a power is a*b, and magnitude (absolute value) is |b:

!5
120
3*2
9
|3J¯4
5

Traditional mathematics also has a complicated and ill-defined order of evaluation. For example, is sine or multiplication evaluated first in sin α φ? How about in sin α cos β? And while functions that supposedly are on the same level evaluate from left to right, this doesn’t apply to exponentiation, and how about a ÷ bx? APL has just one simple rule: All functions take everything on their right as right (or only) argument:

1+2×3
7
2×3+1
8

You may of course use parentheses to force order of evaluation:

(2×3)+1
7

These rules apply to all functions, whether built-in or user-defined. Know the syntax of one, and you know the syntax of all of them!