APL Reference

Overview

APL is an array-oriented programming language. This implementation supports a subset of APL with Unicode glyphs and ASCII aliases. Expressions are evaluated right-to-left: 2×3+4 is 2×(3+4) = 14.

Data Types

All numeric values are 64-bit floating point (f64). Arrays are stored as contiguous vectors with a shape. A scalar has shape [], a vector has shape [n], a matrix has shape [r,c].

Negative number literals use high minus: ¯3 or _3 in ASCII. Integer-valued floats display without decimals.

Syntax

Scalar Functions

Scalar functions apply element-wise to arrays and support scalar extension.

GlyphASCIIMonadicDyadicExample
+Identity (conjugate)Add2+35
-NegateSubtract-3¯3
×mulSignum (¯1, 0, 1)Multiply3×412
÷divReciprocalDivide10÷42.5
*Exponential (ex)Power2*101024
|Absolute valueResidue (modulo)3|71
ceilCeilingMaximum⌈3.24
floorFloorMinimum⌊3.73
=Equal3=31
neNot equal3≠41
<Less than2<31
>Greater than3>1 2 31 1 0
leLess or equal3≤31
geGreater or equal3≥40
landLogical AND1∧00
lorLogical OR0∨11
~Not (logical)Without~01

Structural Functions

GlyphASCIIMonadicDyadicExample
ιiotaIndex generator (1..N)Index ofι51 2 3 4 5
ρrhoShapeReshape2 3ρι6 → 2×3 matrix
revReverseRotate⌽1 2 33 2 1
,Ravel (flatten)Catenate1 2,3 41 2 3 4
takeTake3↑ι51 2 3
dropDrop2↓ι53 4 5
tallyTally (first axis length)≢1 2 3 44

Operators

Operators modify functions to produce new derived functions.

SyntaxNameDescriptionExample
f/ReduceFold function across array (right to left)+/1 2 3 410
f\ScanRunning reduction (prefix sums, etc.)+\1 2 3 41 3 6 10
or f eachEachApply monadic function to each element-¨1 2 3¯1 ¯2 ¯3
∘.fOuter ProductApply dyadic function to all pairs1 2 3∘.×1 2 3 → multiplication table

Special Syntax

SyntaxASCIIDescription
¯_High minus (negative literal): ¯3 or _3
name←exprname gets exprVariable assignment
⎕←exprprint exprPrint result
inputRead next line of input as number(s). Single number → scalar; space-separated numbers → vector; non-numeric → character codes.
(newline)Statement separator
&x235D;NBComment (to end of line)

Examples

Sum of 1 to 100

+/ι100

Result: 5050

Multiplication table

1 2 3 4 5∘.×1 2 3 4 5

Using variables

x←5 ◊ x+3

Result: 8

Sum input numbers

+/⎕

With input 1 2 3 4 5, result: 15

Multiple input reads

⎕ + ⎕

With input 10\n20 (two lines), result: 30

Primes up to N (using ASCII aliases)

n gets 30
nums gets 1 drop iota n
sieve gets (0=nums∘.|nums) +/ each
nums gets (sieve=1) / nums

Matrix reshape and reduce

+/ 2 3ρ1 2 3 4 5 6

Result: 6 15 (row sums)

Constraints