APL Cheat Sheet
Core APL array-language notation covering arithmetic, indexing, custom dfns, and reduction/scan operators for concise array programming.
Basic Arithmetic & Arrays
Scalar and array arithmetic evaluated right-to-left.
2 + 3 ⍝ 5⍳5 ⍝ 1 2 3 4 5 (index generator)2 × ⍳5 ⍝ 2 4 6 8 10V ← 1 2 3 4 5 ⍝ assign a vector⍴V ⍝ 5 (shape of V)V[2] ⍝ 2 (indexing, 1-based by default)
Core Symbols
Primitive glyphs used constantly in APL code.
- ←- assignment, e.g. V ← 1 2 3
- ⍳ (iota)- generates an index vector, e.g. ⍳5 is 1 2 3 4 5
- ⍴ (rho)- monadic: returns shape; dyadic: reshapes an array
- ⌽- reverses the elements of a vector
- ⍉- transposes a matrix
- / (reduce)- inserts a function between elements, e.g. +/1 2 3 sums to 6
- \ (scan)- like reduce but keeps every intermediate result
- ⍺ / ⍵- left and right arguments inside a dfn (curly-brace function)
Custom Functions (dfns)
Defining small anonymous functions with curly braces.
square ← {⍵ × ⍵}square 5 ⍝ 25add ← {⍺ + ⍵}3 add 4 ⍝ 7avg ← {(+/⍵) ÷ ≢⍵}avg 1 2 3 4 ⍝ 2.5
Reduction & Scan
Folding a function across an array's elements.
+/1 2 3 4 ⍝ 10 (sum)×/1 2 3 4 ⍝ 24 (product)+\1 2 3 4 ⍝ 1 3 6 10 (running sum)⌈/3 1 4 1 5 ⍝ 5 (maximum via reduce)
Dfns & Higher-Order Operators
Define inline functions (dfns) and combine them with each (¨), reduce (/), and outer product (∘.f).
square ← {⍵×⍵}square 5 ⍝ 25square ⍳5 ⍝ 1 4 9 16 25avg ← {(+/⍵)÷≢⍵} ⍝ dfn: sum divided by countavg 1 2 3 4 5 ⍝ 31 2 3 +¨ 10 20 30 ⍝ 11 22 33 (each, elementwise pairing)(⍳3) ∘.× ⍳3 ⍝ 3x3 outer product multiplication tablemax ← {⍺>⍵:⍺ ⋄ ⍵} ⍝ dyadic dfn with ⍺ (left) and ⍵ (right) args3 max 7 ⍝ 7
Boolean Masking & Selection
Use comparison operators to build boolean masks, then compress (/) or index (⌿) to filter arrays without explicit loops.
V ← 3 7 1 9 4 6mask ← V > 4 ⍝ 0 1 0 1 0 1mask/V ⍝ 7 9 6 (compress: keep where mask is 1)(~mask)/V ⍝ 3 1 4 (negated mask)⍸ mask ⍝ 2 4 6 (indices where mask is 1, "where")M ← 3 3⍴⍳9(M>4)/,M ⍝ apply mask to a raveled (flattened) matrix1 0 1⌿M ⍝ selects rows 1 and 3 of M
Reshape, Rank & Nested Arrays
Reshape flat data into multi-dimensional arrays and work with nested (non-uniform) arrays.
M ← 3 4⍴⍳12 ⍝ reshape 1..12 into a 3-row, 4-col matrix⍴M ⍝ 3 4≢M ⍝ 3 (tally: count of major cells / rows)⍴⍴M ⍝ 2 (rank of M, i.e. number of dimensions)M[2;] ⍝ entire second rowM[;3] ⍝ entire third columnM[2;3] ⍝ single element, row 2 col 3nested ← (1 2 3)(4 5)(6) ⍝ a nested vector of differently-sized vectors≢nested ⍝ 3⊃nested ⍝ 1 2 3 (disclose/pick the first element)
Tacit Programming (Trains)
Compose functions point-free using trains, avoiding explicit argument names for concise, function-level definitions.
avg ← +/÷≢ ⍝ 3-train (fork): sum, divide, tallyavg 1 2 3 4 5 ⍝ 3range ← ⌈/-⌊/ ⍝ max minus min, as a forkrange 4 9 1 7 ⍝ 8isPalindrome ← ⌽≡⊢ ⍝ reversed equals identityisPalindrome 'level' ⍝ 1\ Atop (2-train): apply g then f, e.g. count-of-uniquescountUnique ← ≢∘∪countUnique 1 2 2 3 3 3 ⍝ 3
Advanced Glyph Reference
Frequently used operators and functions beyond the basics.
- ¨ (each)- applies a function to every element independently
- ⍨ (commute/selfie)- swaps or duplicates arguments, e.g. -⍨ reverses subtraction order
- ∘ (bind/compose)- binds an argument or composes two functions
- ⍣ (power)- applies a function N times, or until a fixed point
- ∪ (unique)- returns the unique elements of an array, order preserved
- ∩ (intersection)- elements common to two arrays
- ⊂ (enclose)- wraps an array as a single nested scalar element
- ⊃ (disclose/pick)- unwraps a nested element, or picks by index
APL expressions evaluate strictly right-to-left with no operator precedence, so read code from right to left to understand it — `2 × 3 + 4` is `2 × (3 + 4)`, giving 14, not 10.