2×3+4
(2×3)+4
2×3 + 414
10
10
BPL requires Python 3.11 or later. On x86-64, it also requires a CPU with AVX2 (x86-64-v3). Intel CPUs have had AVX2 since 2013 and AMD CPUs since 2015.
This installs the bpl command and the basedpl Python package.
Run bpl to open the REPL. Type an expression and press Enter. ⍝ starts a comment.
BPL evaluates right to left. Parentheses group, and so do spaces. BPL evaluates each part between spaces first.
Numbers separated by spaces form a vector. Functions work on every item.
ι generates indices, ← assigns a value to a name, and +/ sums.
To enter ι5, type backtick, iota, then 5. Typing 5 accepts the glyph (symbol) ι and enters the argument 5. Symbol entry also supports abbreviations and Tab completion.
Bare numbers (numbers written without a suffix) are approximate. Use ₓ for exact integers and ᵣ 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 a tolerance of 1E¯14. For example, 0.3=0.1+0.2 is true. See the Language overview and the glyph index.
A list of vectors in brackets is a nested vector. ¨ applies its operand (the function to its left) to each item:
¨ also works with division reduction (÷/):
Let’s create a function to list primes.
A prime has exactly two positive divisors. The candidates 1ₓ↦10 are the numbers 1 to 10. |⊗ is the outer product of remainders (the remainder for every pair of items from its two arguments). ⍨ passes n as both arguments. Each row below marks the multiples of one candidate:
$t $t $t $t $t $t $t $t $t $t
$f $t $f $t $f $t $f $t $f $t
$f $f $t $f $f $t $f $f $t $f
$f $f $f $t $f $f $f $t $f $f
$f $f $f $f $t $f $f $f $f $t
$f $f $f $f $f $t $f $f $f $f
$f $f $f $f $f $f $t $f $f $f
$f $f $f $f $f $f $f $t $f $f
$f $f $f $f $f $f $f $f $t $f
$f $f $f $f $f $f $f $f $f $t
+⌿ sums down each column, counting each candidate’s divisors:
2ₓ= marks the primes. # keeps the items of n that are marked:
In a dfn (a function written in braces), ω is the argument. Given the candidates ↦50ₓ, the numbers 1 to 50, the dfn lists the primes up to 50:
Finally, name it. The trailing ↦ 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, the first two Fibonacci numbers as exact integers. Without the brackets, 15 1 1 would be one vector.{ω,+/¯2↑ω}: Function that appends the sum of the last two items⍣15: Apply the function 15 timesAn array can have keys for its positions and names for its axes. BPL has no separate table type. Here axes maps each axis name to that axis’s position keys, in axis order. axes: then labels the rows by city and the columns by month:
Jan Feb Mar
NY 10 20 30
LA 40 50 60
⌷ selects by key, with one key for each axis. A single key selects a row. ⍠ applies a function along the axes named by its right operand. Summing over month gives one total for each city, keyed by city.
["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 parses CSV text into a vector of columns keyed by the CSV headers. Numeric columns use compact storage (storage that holds items of one type side by side). These two orders total 101:
•json parses JSON objects into the same keyed arrays. Parse a record and select a field:
•json⁻ writes the record back as JSON text. For files, compose the CSV and JSON functions with •nget and •nput, as in •csv •nget "orders.csv". See files, CSV and JSON for dialect and file options.
text •r pattern gives a table of each match’s text, position and groups, and text •replace [pattern template] replaces each match:
Construct a standard normal, then evaluate its CDF and quantiles. Distribution methods accept arrays:
0.1586552539 0.5 0.8413447461
¯1.959963985 0 1.959963985
Pass sample a shape to draw an array of random values. Pass a generator from •rand as the left argument to make the draws repeatable:
1.18069415 0.6528038952 0.1946972875
0.9976188005 ¯0.388864875 ¯1.539001726
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. The main differences from Dyalog are that indices count from 0, brackets are used to write vectors, and spaces group expressions. See the Language overview.