---
title: Getting started
description: 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.

```bash
pip install basedpl
```

This installs the `bpl` command and the `basedpl` Python package.

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## First calculations

Run `bpl` to open the [REPL](repl.md). Type an expression and press Enter. [`⍝`](https://bpl.fast.ai//glyphs/lamp.html) starts a comment.

BPL evaluates right to left. Parentheses group, and so do spaces. BPL evaluates each part between spaces first.

```bpl
2×3+4
(2×3)+4
2×3 + 4
```

```output
14
10
10
```

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

```bpl
10+1 2 3
```

```output
11 12 13
```

[`ι`](https://bpl.fast.ai//glyphs/iota.html) generates indices, [`←`](https://bpl.fast.ai//glyphs/left-arrow.html) assigns a value to a name, and `+/` sums.

```bpl
v←ι5 ◇ v
+/ι5
```

```output
0 1 2 3 4
10
```

To enter `ι5`, type backtick, `iota`, then `5`. Typing `5` accepts the [glyph](terms.md#term-glyph) (symbol) [`ι`](https://bpl.fast.ai//glyphs/iota.html) and enters the argument `5`. [Symbol entry](repl.md#typing-glyphs) also supports abbreviations and Tab completion.

## Numbers and arrays

[Bare numbers](terms.md#term-bare-number) (numbers written without a suffix) are approximate. Use `ₓ` for exact integers and `ᵣ` for fractions.

```bpl
1÷3
1ᵣ÷3ᵣ
```

```output
0.3333333333
1ᵣ3
```

`j` separates real and imaginary parts.

```bpl
1j2×1j¯2
```

```output
5ⱼ0
```

[`ρ`](https://bpl.fast.ai//glyphs/rho.html) reshapes a vector; `+/` sums each row.

```bpl
+/2 3ρι6
```

```output
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](overview.md) and the [glyph index](glyphs.md).

## Nested arrays

A list of vectors in brackets is a nested vector. [`¨`](https://bpl.fast.ai//glyphs/diaeresis.html) applies its operand (the function to its left) to each item:

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

```output
3 12
```

[`¨`](https://bpl.fast.ai//glyphs/diaeresis.html) also works with division reduction (`÷/`):

```bpl
÷/¨n
```

```output
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). [`⍨`](https://bpl.fast.ai//glyphs/tilde-diaeresis.html) passes `n` as both arguments. Each row below marks the multiples of one candidate:

```bpl
n←1ₓ↦10
0ₓ=|⊗⍨n
```

```output
$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:

```bpl
+⌿0ₓ=|⊗⍨n
```

```output
[1 2 2 3 2 4 2 4 3 4]ₓ
```

`2ₓ=` marks the primes. [`#`](https://bpl.fast.ai//glyphs/hash.html) keeps the items of `n` that are marked:

```bpl
2ₓ=+⌿0ₓ=|⊗⍨n # n
```

```output
[2 3 5 7]ₓ
```

In a [dfn](dfns.md) (a function written in braces), [`ω`](https://bpl.fast.ai//glyphs/omega.html) is the argument. Given the candidates `↦50ₓ`, the numbers 1 to 50, the dfn lists the primes up to 50:

```bpl
{2ₓ=+⌿0ₓ=|⊗⍨ω # ω}↦50ₓ
```

```output
[2 3 5 7 11 13 17 19 23 29 31 37 41 43 47]ₓ
```

Finally, name it. The trailing [`↦`](https://bpl.fast.ai//glyphs/right-arrow-from-bar.html) generates the candidates:

```bpl
primes←{2ₓ=+⌿0ₓ=|⊗⍨ω # ω}↦
primes 50ₓ
```

```output
[2 3 5 7 11 13 17 19 23 29 31 37 41 43 47]ₓ
```

The built-in prime glyph [`⍭`](https://bpl.fast.ai//glyphs/stile-tilde.html) returns the prime at a position, counting from 0. Applied to `ι15ₓ`, it produces the same list directly:

```bpl
⍭ ι15ₓ
```

```output
[2 3 5 7 11 13 17 19 23 29 31 37 41 43 47]ₓ
```

The Fibonacci sequence:

```bpl
{ω,+/¯2↑ω}⍣15 [1 1]ₓ
```

```output
[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:

```bpl
axes←["city":"NY" "LA";"month":"Jan" "Feb" "Mar"]
sales←axes:[10 20 30◇40 50 60]
sales
```

```output
   Jan Feb Mar
NY  10  20  30
LA  40  50  60
```

[`⌷`](https://bpl.fast.ai//glyphs/squad.html) selects by key, with one key for each axis. A single key selects a row. [`⍠`](https://bpl.fast.ai//glyphs/quad-colon.html) applies a function along the axes named by its right operand. Summing over `month` gives one total for each city, keyed by city.

```bpl
"LA"⌷sales
"LA" "Feb"⌷sales
+/⍠"month" sales
```

```output
["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](keyed.md) 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](terms.md#term-compact-storage) (storage that holds items of one type side by side). These two orders total 101:

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

```output
101
```

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

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

```output
"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](data.md) for dialect and file options.

```bpl
•json⁻ record
```

```output
"{""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:

```bpl
codes←"([A-Z]+)-([0-9]+)"
"AB-12 CD-3" •r codes
"AB-12 CD-3" •replace [codes "$2:$1"]
```

```output
["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:

```bpl
normal←•distribution "normal"
normal.cdf ¯1 0 1
normal.quantile 0.025 0.5 0.975
```

```output
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:

```bpl
•rand1 normal.sample 2 3
```

```output
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:

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

```output
0.25 0.5 0.25
```

See [distributions](distributions.md) for the 17 families, and [regex](regex.md) for captures and replacement options.

## Learning APL

To start learning APL, follow the [17 video series](https://forums.fast.ai/t/apl-array-programming/97188) run by Jeremy Howard, and have a look at the [study notes](https://fastai.github.io/apl-study/apl.html). 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](overview.md).
