Skip to main content

Getting started

Calculate with arrays, then build functions.

Install

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.

pip install basedpl

This installs the bpl command and the basedpl Python package.

First calculations

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.

2×3+4
(2×3)+4
2×3 + 4
14
10
10

Numbers separated by spaces form a vector. Functions work on every item.

10+1 2 3
11 12 13

ι generates indices, ← assigns a value to a name, and +/ sums.

v←ι5 ◇ v
+/ι5
0 1 2 3 4
10

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.

Numbers and arrays

Bare numbers (numbers written without a suffix) are approximate. Use ₓ for exact integers and ᵣ for fractions.

1÷3
1ᵣ÷3ᵣ
0.3333333333
1ᵣ3

j separates real and imaginary parts.

1j2×1j¯2
5ⱼ0

ρ reshapes a vector; +/ sums each row.

+/2 3ρι6
3 12

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.

Nested arrays

A list of vectors in brackets is a nested vector. ¨ applies its operand (the function to its left) to each item:

n←[[1 2] [3 4 5]]
+/¨n
3 12

¨ also works with division reduction (÷/):

÷/¨n
0.5 3.75

Example algorithms

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:

n←1ₓ↦10
0ₓ=|⊗⍨n
$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:

+⌿0ₓ=|⊗⍨n
[1 2 2 3 2 4 2 4 3 4]ₓ

2ₓ= marks the primes. # keeps the items of n that are marked:

2ₓ=+⌿0ₓ=|⊗⍨n # n
[2 3 5 7]ₓ

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:

{2ₓ=+⌿0ₓ=|⊗⍨ω # ω}↦50ₓ
[2 3 5 7 11 13 17 19 23 29 31 37 41 43 47]ₓ

Finally, name it. The trailing ↦ generates the candidates:

primes←{2ₓ=+⌿0ₓ=|⊗⍨ω # ω}↦
primes 50ₓ
[2 3 5 7 11 13 17 19 23 29 31 37 41 43 47]ₓ

The built-in prime glyph ⍭ returns the prime at a position, counting from 0. Applied to ι15ₓ, it produces the same list directly:

⍭ ι15ₓ
[2 3 5 7 11 13 17 19 23 29 31 37 41 43 47]ₓ

The Fibonacci sequence:

{ω,+/¯2↑ω}⍣15 [1 1]ₓ
[1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597]ₓ

Explanation:

  1. [1 1]ₓ: Initial seed, the first two Fibonacci numbers as exact integers. Without the brackets, 15 1 1 would be one vector.
  2. {ω,+/¯2↑ω}: Function that appends the sum of the last two items
  3. ⍣15: Apply the function 15 times

Labelled arrays

An 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:

axes←["city":"NY" "LA";"month":"Jan" "Feb" "Mar"]
sales←axes:[10 20 30◇40 50 60]
sales
   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.

"LA"⌷sales
"LA" "Feb"⌷sales
+/⍠"month" sales
["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 and JSON

•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:

orders←•csv "price,qty
10.5,2
20,4"
+/orders.price×orders.qty
101

•json parses JSON objects into the same keyed arrays. Parse a record and select a field:

record←•json "{""name"":""Ada"",""scores"":[8,9,10]}"
record.name
+/record.scores
"Ada"
27ₓ

•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.

•json⁻ record
"{""name"":""Ada"",""scores"":[8,9,10]}"

Regex

text •r pattern gives a table of each match’s text, position and groups, and text •replace [pattern template] replaces each match:

codes←"([A-Z]+)-([0-9]+)"
"AB-12 CD-3" •r codes
"AB-12 CD-3" •replace [codes "$2:$1"]
["text":["AB-12" "CD-3"] "position":[0 6]ₓ "groups":("AB" "12"◇"CD" "3")]
"12:AB 3:CD"

Probability distributions

Construct a standard normal, then evaluate its CDF and quantiles. Distribution methods accept arrays:

normal←•distribution "normal"
normal.cdf ¯1 0 1
normal.quantile 0.025 0.5 0.975
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:

•rand1 normal.sample 2 3
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:

coin←2 0.5 •distribution "binomial"
coin.density 0 1 2
0.25 0.5 0.25

See distributions for the 17 families, and regex for captures and replacement options.

Learning APL

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.