2×3+4 ⍝ 14
(2×3)+4 ⍝ 10
2×3 + 4 ⍝ 1014
10
10
Requires Python ≥3.10.
This installs the bapl command and the basedpl Python package.
Run bapl to open the REPL. Type an expression and press Enter. ⍝ introduces a comment; examples here use it to show the result.
APL evaluates right-to-left. Parentheses change grouping, and so do spaces: each part between spaces is evaluated first.
Numbers separated by spaces form a vector. Functions work on every element.
⍳ generates indices, ← assigns a name, and +/ sums.
To enter ⍳5, type backtick, iota, then 5. The digit accepts the glyph and enters the argument. Symbol entry also supports abbreviations and Tab completion.
Bare numbers are approximate. Use x for exact integers and r for fractions.
j separates real and imaginary parts.
⍴ reshapes a vector; +/ sums each row.
Indices start at 0, and negative indices count from the end. Comparisons use tolerance 1E¯14; 0.3=0.1+0.2 is true. See language rules and the glyph index.
Brackets write a vector, so brackets round vectors make a nested vector. Each (¨) applies its operand to each element:
The same Each/reduction pattern works with division:
Let’s create a function to list primes.
A prime has exactly two positive divisors. |⌝ forms an outer product of remainders; ⍨ supplies the same argument on both sides. Indices start at 0, so the candidates are 1+⍳10. Each row below marks the multiples of one candidate:
1ₓ 1ₓ 1ₓ 1ₓ 1ₓ 1ₓ 1ₓ 1ₓ 1ₓ 1ₓ
0ₓ 1ₓ 0ₓ 1ₓ 0ₓ 1ₓ 0ₓ 1ₓ 0ₓ 1ₓ
0ₓ 0ₓ 1ₓ 0ₓ 0ₓ 1ₓ 0ₓ 0ₓ 1ₓ 0ₓ
0ₓ 0ₓ 0ₓ 1ₓ 0ₓ 0ₓ 0ₓ 1ₓ 0ₓ 0ₓ
0ₓ 0ₓ 0ₓ 0ₓ 1ₓ 0ₓ 0ₓ 0ₓ 0ₓ 1ₓ
0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 1ₓ 0ₓ 0ₓ 0ₓ 0ₓ
0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 1ₓ 0ₓ 0ₓ 0ₓ
0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 1ₓ 0ₓ 0ₓ
0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 1ₓ 0ₓ
0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 0ₓ 1ₓ
+⌿ sums down the rows, counting each candidate’s divisors:
2= marks primes. Where (⍸) returns their positions. An array next to an argument selects from it, so n applied to the positions gives the primes:
A dfn names its argument ⍵. Given the candidates 1+⍳50, it lists the primes up to 50:
Finally, name it. The trailing 1+⍳ generates the candidates:
The built-in prime glyph ℙ returns the prime at a position, counting from 0. Applied to ⍳15, it produces the same list directly:
The fibonacci sequence:
Explanation:
[1 1]: Initial seed (first two Fibonacci numbers). Without the brackets, 15 1 1 would be one vector.{⍵,+/¯2↑⍵}: Function that appends the sum of the last two elements⍣15: Apply the function 15 timesArrays can carry keys for positions and names for axes, without becoming a separate table type. A keyed vector pairs each axis name with its position keys, in axis order. Here axes: labels the rows by city and the columns by month:
Jan Feb Mar
NY 10 20 30
LA 40 50 60
sales applied to a key selects that row. ⌷ selects positions by key, with one key for each axis. ⍠ applies a function along the axes its operand names. Summing over month leaves a total for each city, and keeps its labels.
["month":3]⍴["Jan":40 "Feb":50 "Mar":60]
50
["city":2]⍴["NY":60 "LA":150]
Unlabelled arrays keep their usual positional behaviour. See Axis keys for construction, updates and alignment rules.
CSV headers become keys on a vector of columns. Numeric columns use compact storage. These two orders total 101:
JSON objects use the same keyed arrays. Parse a record and select a field:
•tojson converts back to JSON text. For files, compose with •nget and •nput, as in •csv •nget "orders.csv". See files, CSV and JSON for dialect and file options.
•r compiles a Rust regex into functions for matching, positions, groups and replacement:
Construct a standard normal, then evaluate its CDF and quantiles. Distribution methods accept arrays:
0.15865525394505725 0.5 0.8413447460549428
¯1.9599639845400545 0 1.9599639845400538
Sampling takes a shape. A generator from •rand on the left makes the draws repeatable:
0.8439986136221761 0.7155607127305931 ¯1.9158714436187585
0.45249647038446317 ¯0.815791206711391 0.3583251371673788
For discrete distributions, density gives probability mass. A fair coin tossed twice has probabilities ¼, ½, ¼ for zero, one or two heads:
See distributions for the 17 families, and regex for captures and replacement options.
To start learning APL, follow the 17 video series run by Jeremy Howard, and have a look at the study notes. These use Dyalog APL. bAsedPL differs: indices count from 0, brackets write vectors, and spaces group expressions. See the language rules.