install and use: installing apl | installing the mac input method | using the input method on mac | installing the windows input method | using the input method on windows
apl notation: operator reference | tutorial
Input methods for entering APL symbols using ASCII notation.
Input methods are provided for Mac OS X and Emacs.
When an input method is in effect, one types the ASCII notation and it is rendered to the APL symbol on the fly. Here are some examples:
@times ×
@grade ⍋
@reverse ⌽
@domino ⌹
On Mac OS X one can use Homebrew:
$ brew install gnu-apl
Requires Mac OS 10.5 or higher.
$ curl https://raw.githubusercontent.com/clarkgrubb/latex-input/master/apl/apl.cin > ~/Library/Input\ Methods/apl.cin
Optionally, you can install an input method which combines the LaTeX and APL input methods:
$ curl https://raw.githubusercontent.com/clarkgrubb/latex-input/master/apl/latex_apl.cin > ~/Library/Input\ Methods/latex_apl.cin
In
System Preferences | Keyboard | Input Sources
you should see a new input source called APL
. An input source
is what Mac OS X calls an input method. If you check the box next
to APL
and if you make sure that Show Input menu in menu bar
is
checked, then you can use the menu bar to set or unset APL as the
input source.
When you type APL notation it will be underlined. When the notation is finished, type SPACE to render it or RETURN to use it without rendering it. SPACE and RETURN are special when used in this manner and don't insert a space or a newline in the document. If you want a space or newline you must press SPACE or RETURN again.
There is a Mac OS X keyboard shortcut for switching input source, but the default value conflicts with Spotlight. Consider going to
System Preferences | Keyboard | Keyboard Shortcuts | Keyboard & Text Input
and changing it to something like ⌥⌘Space
Download apl.exe and install it in your Startup folder. Open the Startup folder in File Explorer by pressing Cmd+R
and entering shell:startup
. The input method will run the next time you log in. You can also start it by double-clicking it in File Explorer.
If you have AutoHotkey installed, you can download and install apl.ahk instead.
The input method starts in an inactivate state. Use Ctrl+Alt+L
to toggle between active and inactive.
When the input method is active, type APL notation followed by a SPACE
to render a symbol.
parsing | operators | lists | arithmetic | comparison | logic | transcendental | truncation | 1d array | 2d array | strings | higher order operators
Alphanumeric characters other than ¯⎕'_.()
are built-in operators and parse as a single token. APL processes tokens from right to left. Parens ()
can be used like in other languages to change the order of execution.
@macron ¯
@leftarrow ←
@quad ⎕
@lamp ⍝
Positive integers and floating point numbers are like in other languages. The macron ¯
or high minus is used in literals for negative numbers. An uppercase E
is used for scientific notation: e.g. 6.022E23
. An uppercase J
is used to separate the real and imaginary part of a complex number: e.g. 2J1
.
The left arrow ←
assigns the value of the expression on the right to the name on the left. Names must start with a letter or underscore and can contain digits. APL is case sensitive.
The quad ⎕
is an extra alphabetical symbol used in names which are predefined by APL. ⎕av
contains the alphabet recognized by APL.
The lamp ⍝
starts a comment which ends at the end of the line.
@del ∇
APL operators are represented by single characters. They can be either binary infix operators, which APL calls dyadic, or unary prefix operators, which APL calls monadic. Many operators can be used either as a dyadic or a monadic operator with different effect. Which way the operator is used depends on how APL parses the expression. We use α
and ω
to refer to the left and right arguments of operators.
The del ∇
is used to define new monadic or dyadic operators:
∇r ← double n
r ← 2 × n ∇
∇r ← a add b
r ← a + b ∇
Invoking the operators:
double 7
3 add 7
The operands of APL operators are lists. The elements of lists are numeric literals, names, or strings. The elements are usually separated by spaces. There are no delimiters to mark the edges of the list; APL knows it has encountered the left edge of a list when it encounters an operator. Whether the operator is dyadic or monadic depends on whether a list or another operator is found to the left.
@plus +
@mate +
@minus -
@times ×
@trend ×
@per ÷
@residue |
@size |
The arithmetic operators can be dyadic or monadic. Their use is like in other languages.
The monadic mate +
is the complex conjugate function.
The monadic trend ×
is the signum function.
The monadic per ÷
returns the reciprocal of its argument.
The residue |
is like the modulus operator %
of other languages, but order of the arguments is reversed.
Monadic size |
is the absolute value function. | 3J4
is 5
.
@aft ≥
@after >
@before <
@equal =
@fore ≤
@unequal ≠
@match ≡
The above operators are exclusively dyadic in GNU APL. They are comparison operators. They return 1
or 0
, which APL uses to represent true and false. =
, <
, and >
are ASCII characters.
The dyadic match ≡
returns a single boolean value when comparing two lists. The dyadic equal =
, by contrast, performs elementwise comparison and returns a list of boolean values.
@not ~
@and ∧
@or ∨
@nand ⍲
@nor ⍱
Monadic not ~
is logical negation. A domain error results if the argument is not a boolean, i.e. 1
or 0
.
And ∧
and or ∨
are exclusively dyadic. On boolean arguments they are logical operators. On other integer arguments they are the least common multiple and greatest common divisor, respectively.
@power *
@log ⍟
@circle ○
@pi ○
@outof !
@factorial !
Monadic power *
is the natural exponential function.
Dyadic log ⍟
is the logarithmic function, where the left operand is the base and the right operand is the argument. Monadic log is the natural logarithm.
Dyadic circle ○
is used for the trigonometric functions. If the first argument is 1
, 2
, or 3
, then the sin, cos, or tan of the second argument is returned. If the first argument is ¯1
, ¯2
, or ¯3
, then the arcsin, arccos, or arctan of the second argument is returned. Monadic circle ○
returns the argument times π
.
Dyadic factorial !
is the function 1/((ω + 1) ⋅ Beta(ω - α + 1, α + 1))
. When the arguments are integers this is ω
choose α
.
Monadic factorial !
is the function Γ(ω + 1)
. When the argument is an integer it is the factorial function.
@left ⊣
@right ⊢
@maximum ⌈
@minimum ⌊
@ceiling ⌈
@floor ⌊
@format ⍕
Left ⊣
and right ⊢
are exclusively dyadic operators. They return their left and right arguments, respectively.
The dyadic maximum ⌈
and minimum ⌊
operators return the greater and the lesser of their arguments, respectively. When used as monadic operators they are ceiling and floor functions. That is, they return the least integer greater than or equal to the argument or the greatest integer less than or equal to argument, respectively.
The dyadic format ⍕
rounds its right argument to the precision specified on the left. The monadic format ⍕
uses the built-in name ⎕pp
.
@less ~
@index ⍳
@count ⍳
@deal ?
@roll ?
@rotate ⌽
@reverse ⌽
@drop ↓
@take ↑
@by ,
@grade ⍋
@downgrade ⍒
@in ∊
Dyadic less ~
is set difference: the elements on the left with any elements on the right removed.
Dyadic index ⍳
computes the one-based index of the first occurrence of the left argument in the list on the right. Monadic count ⍳
takes a nonnegative integer as an argument and returns the list of integers from 1
to ω
.
Dyadic deal ?
select α
random numbers without replacement from the list of integers from 1
to ω
. Monadic roll ?
generates a random integer from 1
to ω
.
Dyadic rotate ⌽
pops elements off the front of the list ω
and pushes them onto the back when α
is positive. When α
is negative it pops them off the back and pushes them onto the front. Monadic reverse ⌽
reverses a list. On a 2-dimensional array it reverses each row.
Dyadic drop ↓
returns the list ω
with the first α
items removed. α
can be negative, in which case items are removed from the end.
Dyadic take ↑
returns the first α
items from the list ω
. α
can be negative, in which case items are returned from the end.
Dyadic by ,
concatenates the lists α
and ω
.
Monadic grade ⍋
returns the ascending rank order of the elements of ω
.
Monadic downgrade ⍒
returns the descending rank order of the elements of ω
.
@reshape ⍴
@shape ⍴
@upset ⊖
@rowel ⊖
@cant ⍉
@take ↑
@ravel ,
@inverse ⌹
@domino ⌹
Dyadic reshape ⍴
converts the elements of ω
into a multidimensional array with the shape α
. Monadic shape ⍴
returns the length of an array, or the dimensions of a multidimensional array.
Monadic upset ⊖
reverses the elements in each column of a 2-dimensional array.
Monadic cant ⍉
transposes a 2-dimensional array.
Dyadic take ↑
returns a corner subarray from the 2-dimensional array ω
of size determined by α
. If all elements of α
are positive, the subarray is taken from the upper left corner.
Monadic ravel ,
converts a 2-dimensional array into a 1-dimensional array using row major order.
Monadic inverse ⌹
returns the inverse of a 2-dimensional array. Dyadic domino ⌹
solves the linear equation Ax = y
, where ω
is A
and α
is y
.
Strings are single quoted '
and doubling is used to insert a single quote character in a string.
@execute ⍎
@dieresis ¨
@period .
@paw ⍤
@cup ∪
@cap ∩
@slashbar ⌿
@backslashbar ⍀
@all {
@antibase ⊤
@base ⊥
@box <
@box ⊃
@cycle ≤
@from {
@mix ≥
@nubin =
@nubsieve ≠
@open >
@over ⍪
@raze ↓
@table ⍪
APL calls unary operators monadic and binary operators dyadic. Monadic operators are prefix and dyadic operators are infix. Most APL operators take lists as arguments, but there are higher order operators which take other operators as arguments. And example of such an operator is /, which acts like reduce. The following expression uses addition to reduce the list `[1, 2, 3]1 and evaluates to 6:
+ / 1 2 3
True and false are 1 and 0.
Some operators can be used as both monadic and dyadic operators. In addition to minus, which works like other languages, the ∣ operator (U+2223) is absolute value when monadic and modulus when dyadic. ⋆ (U+22C6) is the exponential operator when monadic and the power operator when dyadic. The ○ operator (U+25CB) is a multiple of π when monadic and a trigonometric operator when dyadic, with the first argument determining which trigonometric function.
Most APL functions work on arrays. The following performs elementwise addition to give 3 5 7
:
1 2 3 + 2 3 4
This performs elementwise multiplication to give 2 6 12
:
1 2 3 × 2 3 4
The dot product of the two vectors is sum of the elementwise multiplication, which is 20. In APL we can compute it with:
+ / 1 2 3 × 2 3 4
The above uses the reduce operator /. It also uses the fact that APL expressions are parsed from right to left.
Another way to compute the dot product is:
1 2 3 +.× 2 3 4
The . operator is called the inner product operator. It takes two dyadic operators as its arguments and returns a dyadic operator.
When a scalar is added to an array, the scalar is added to each element of the array. This expression evaluates to 2 3 4
:
1 + 1 2 3
Otherwise, adding arrays of different length does not work. This results in a LENGTH ERROR:
1 1 + 1 2 3
Matrices are created by reshaping an array. This APL expression
2 2 ⍴ 1 2 3 4
creates the matrix:
1 2
3 4
To create a 2x2
matrix of all zeros or all ones:
2 2 ⍴ 0
2 2 ⍴ 1
To add two matrices to get a 2x2
matrix of all fives:
( 2 2 ⍴ 1 2 3 4 ) + 2 2 ⍴ 4 3 2 1
Again, APL expressions are parsed from right to left. The parens, which work like other languages, are necessary to prevent the APL interpreter from trying to add a 2x2 matrix with the array 1 2 3 4
.
Matrix multiplication is performed like the dot product of arrays:
( 2 2 ⍴ 1 2 3 4 ) +.× 2 2 ⍴ 4 3 2 1
- monadic minus vs high minus
- prefix sum +@ (scan)
- range
- random (roll deal)
- take / drop
- concat / ravel ,
- min/max (use floor ceil)
- membership
- decode / encode
- matrix inverse
- general transpose
- reverse / rotate
- grade up / grade down
- factorial / combinations
- outer product
GNU APL errors:
- VALENCE ERROR
- LENGTH ERROR
- RANK ERROR
- DOMAIN ERROR
- INDEX ERROR