11
Run these examples in the playground, the interactive prompt (REPL), or a BPL notebook. Work through them in order, trying a few changes of your own.
A glyph is a written symbol, such as +. To get help on one at the REPL, type ]help +. The glyph reference has the full definitions.
BPL works with values such as numbers and arrays. Start by entering a few numbers.
Write positive numbers, negative numbers and decimals directly. A negative number starts with the high bar ¯. A value written directly in code, such as 1.5, is called a literal.
Bare numbers use approximate arithmetic. Append ₓ for an exact integer. Write an exact fraction with r between its numerator and denominator, as in 1r3.
Exact fractions display with ᵣ between the numerator and denominator. This spelling also works in input.
An e, E or ₑ between two numbers means the first number times 10 raised to the second. A negative exponent moves the decimal point left.
A complex number has a real part and an imaginary part. Write j between them. The imaginary part multiplies i, the square root of −1.
Complex numbers display with ⱼ in place of the input’s j. Either spelling works in input.
3j4 means 3+4i.
4j0 means 4+0i, which is just the real number 4
A float is an approximate real number. A bit stores either 0 or 1. Bare floats use 64 bits. The suffixes ₕ, ₛ and ₚ explicitly choose 16, 32 and 64 bits. Fewer bits use less memory and hold fewer significant digits. A narrow literal is rounded when read.
Complex numbers have two parts, each using 32 or 64 bits. ₛ and ₚ choose those widths. ₕ on a complex number also gives 32-bit parts.
An array stores values, called items, at numbered positions. A vector has one axis: its positions run in one direction. Positions count from 0. An array’s rank is its number of axes; a vector has rank 1.
Put a vector’s items in brackets, separated by spaces. A list of literals separated by spaces is a shorthand for the same vector.
Append ₓ to a bracketed numeric array to make all its numbers exact:
The width suffixes also apply to bracketed numeric arrays.
An item of an array can itself be an array. Write nested vectors with nested brackets:
⍝ starts a comment. Everything after it on the same line is ignored.
A function calculates a result from one or two values called arguments. + adds its arguments and × multiplies them. Functions apply from right to left, with no precedence between them. In 3×2+1, the addition happens first.
A group in parentheses is calculated as one part. (2×3)+4 multiplies first, then adds 4.
Spacing can make the same grouping. A run is code with no spaces between its parts. A group in parentheses or brackets counts as one part, even when it contains spaces. Each run is calculated as one part of the surrounding expression.
2×3 + 4 has three runs: 2×3, +, and 4. It gives the same result as (2×3)+4.
Without the space, addition happens first:
A list of literals forms a vector before the functions apply. 1 2 3×2 doubles the vector 1 2 3. You can also write [1 2 3]×2.
In 1 2 3×2+1, add 2+1 first, then multiply each item of 1 2 3 by the result.
A function used with one argument, to its right, is monadic. Used with an argument on each side, it is dyadic. The same glyph can name a different function in each form. For example, monadic - negates a number and dyadic - subtracts.
- (Minus sign; Bar)- (Negate)Negate changes the sign of each number. Positive numbers become negative and negative numbers become positive.
⁻¹ after a function asks for its inverse, a function that undoes it. Negating twice restores the input, so Negate is its own inverse.
After a number, the same ⁻¹ means its reciprocal: one divided by that number.
- (Minus; subtract)Subtraction works element by element, pairing the items at each position.
A single number extends across a vector for element-wise arithmetic.
With a left argument, an inverse keeps that argument fixed and finds the right argument. 10-⁻¹3 is 7 because 10-7 is 3.
+ (Plus sign)+ (Conjugate)Conjugate negates the imaginary part of a complex number. It leaves real numbers unchanged.
Conjugate is its own inverse too.
+ (Plus)Plus works element-wise just like minus.
The inverse of adding 3 subtracts 3.
| (Stile;Vertical Bar)| (Magnitude)Magnitude is distance from zero. For a real number it removes the sign. The complex number 3j4 has magnitude 5, the distance from the origin to the point (3, 4).
| (Residue;Modulus)Residue, also called modulus, gives the remainder after division. 2|7 is 1 because 7 divided by 2 is 3 with a remainder of 1.
With negative arguments, the nonzero remainder has the same sign as the left argument. For example, 10|¯13 is 7 because ¯13 is ¯2×10 plus 7.
÷ (Divide sign)÷ (Reciprocal)Reciprocal is division where 1 is the left argument.
÷ 3 is equivalent to 1 ÷ 3÷ ¯2 is equivalent to 1 ÷ ¯2÷ 3j4 is equivalent to 1 ÷ 3j4Reciprocal is its own inverse.
÷ (Divided by)Divided by does element-wise division
Arithmetic on exact numbers stays exact:
With a fixed numerator, inverse division finds the denominator.
× (Times sign)× (Direction)For real numbers: 1 for positive numbers, ¯1 for negative numbers, 0 for 0.
For a nonzero complex number, Direction divides the number by its magnitude. The result points in the same direction and has magnitude 1.
Direction loses the magnitude of its input. Its inverse returns the direction unchanged, choosing a number of magnitude 1, or zero. The argument must already be a possible direction.
× (Times)Element-wise multiplication
The inverse of multiplying by 4 divides by 4. A zero multiplier has no inverse.
* (Star)* (Exponential)Exponential raises the constant e (approximately 2.71828) to the power of its argument. *1 gives e.
The inverse exponential finds the exponent that produced its argument. This is called the natural logarithm.
* (Power)Power raises the left argument to the right argument. It works element by element.
The reciprocal of 3 gives the exponent for a cube root:
With a fixed base, the inverse finds the exponent. Here 2 to the power of 3 is 8.
An integer written above the line, a superscript, gives a power. ² squares its argument and ³ cubes it. Several digits and a leading superscript minus are allowed.
A touching superscript applies to the value just before it. In 2 3², only 3 is squared. Brackets make the whole vector one value.
A space before a superscript applies it to the whole run on its left. A spaced superscript also needs a space after it.
Superscripts apply before functions. -3² negates the square, whereas (-3)² squares the negated number.
√ (Root)Monadic √ takes a square root. Dyadic √ takes the root whose degree is on the left.
Inverse square root squares its argument. With a fixed degree on the left, inverse Root raises its right argument to that degree.
! (Exclamation mark)! (factorial)5 Factorial, or !5 is equal to 5×4×3×2×1.
Factorial also extends to non-integer arguments:
! (Binomial)X!Y counts selections of X things from Y things, ignoring their order. The following counts selections of 0 through 5 things from 5 things.
¿ (Inverted question mark)¿ (Roll)Roll gives a random integer from 0 up to, but not including, its argument. With 0 it gives a random number between 0 and 1.
¿ (Deal)Deal draws without replacement. 13¿52 gives 13 distinct integers from 0 to 51.
π (Pi Times)π (Pi times)The constant π is approximately 3.14159. Monadic π multiplies its argument by π.
π⁻¹ divides by π.
π (Pi ratio)With two arguments, XπY is X times π divided by Y. Its inverse with fixed X uses the same calculation.
○ (Circle)○ (Cis)Measure angles in radians: one full turn is 2π radians. Cis turns an angle into the complex point at that angle on a circle of radius 1. Its real part is the cosine of the angle and its imaginary part is the sine.
The inverse, ○⁻¹, turns a point on that circle back into an angle between −π and π. It cannot recover whole turns.
○ (Circular functions)The left argument selects a circular function. Code 1 gives Sine and code 2 gives Cosine.
The same code with ⁻¹ selects the inverse function. Inverse sine is called arcsine. It finds an angle whose sine is the given number. For real inputs from ¯1 to 1, it chooses an angle between −π/2 and π/2.
Hyperbolic functions combine exponentials. Code 5 gives hyperbolic sine, or sinh: half the difference between the exponential of the argument and the exponential of its negation.
Its inverse is called inverse hyperbolic sine, or arcsinh.
Code 0 takes the square root of 1 minus the square of its argument. Its inverse uses the same calculation.
The Circle reference lists every code and its inverse.
⍟ (Logarithm; Circle Star)⍟ (Natural Log)A logarithm gives the power needed to produce a number from a chosen base. The natural logarithm uses e as its base. For example, the natural logarithm of e is 1.
The inverse of the natural logarithm is Exponential.
⍟ (Log)The left argument chooses the base. 2⍟32 is 5 because 2 to the power of 5 is 32.
With a fixed base, inverse Log raises that base to the given power.
The left arrow ← assigns a value to a name. In a←3, the name a refers to 3. A name whose last character is a digit needs an underscore before the digits: x_1, not x1.
Assigning to ⎕ prints a value. Chained assignments still run right to left: first assign 3 to a, then print it.
The subscript letters also join values from expressions into one number. aⱼb has real part a and imaginary part b. aₑb is a times 10 raised to b. aᵣb makes the exact fraction a over b, rounding both down first if needed. Each letter must touch the values on both sides.
A group can supply either part. The joined number is calculated before the surrounding functions.
A function written in curly braces is called a dfn. Its contents are its body. In a monadic call, the dfn receives its right argument as ⍵.
In a dyadic call, the dfn also receives a left argument, ⍺.
A statement is one step of a calculation. Separate statements with a newline or ⋄. A dfn returns the result of its last statement. In this example, first assign ⍵×2 to a, then add 2.
A predicate chooses whether a dfn body runs. Write the condition followed by ?, then the statements to run when it is true. Separate alternative bodies with ;.
> means greater than. In {⍵>1?1;⍵}, an argument greater than 1 gives the result 1. Otherwise the next body returns the argument unchanged.
≤ means less than or equal to. This factorial function returns 1 for arguments at most 1. Otherwise it multiplies the argument by the factorial of one less. Calling the same function again is called recursion.
Inside a dfn, ∇ refers to that same dfn. It lets you write a recursive call without using the function’s name.
The two definitions give the same results:
Destructuring assigns the items of an array to separate names. Put the names in brackets on the left of ←.
⍴ (rho)⍴ (reshape)A vector has one axis. A matrix has two, with items arranged in rows and columns. Its shape is the length of each axis, written as a vector. A matrix with 2 rows and 3 columns has shape 2 3.
Dyadic ⍴ reshapes its right argument to the shape given on the left.
Reshape cycles through the original vector as needed to fill the desired shape.
With a shape on the left, ⍴⁻¹ reads the items back into a vector in row order. Its right argument must have the given shape. It cannot restore items discarded by an earlier reshape.
⍴ (shape of)Monadic ⍴ gives an array’s shape.
A number is an atom: a value that isn’t an array. It has no axes and its shape is the empty vector, written ⍬.
Suffixes work on the empty vector too. ⍬ₓ is an empty vector of exact numbers; ⍬ₕ, ⍬ₛ and ⍬ₚ choose a float width.
The empty vector still has one axis, of length 0:
The shape is itself a vector. Taking its shape gives a one-item vector containing the rank of the original array.
A character is an atom written in single quotes, such as 'A'. A string is a vector of characters written in double quotes, such as "ACF". Its length is its number of characters.
A character has no axes. The string "y" is a vector of length 1.
A string inside another vector is a nested array, like the numeric vectors earlier.
┌→─────────────────┐
│ ┌→──┐ ┌→──┐ ┌→─┐ │
│ │1 2│ │3 4│ │ab│ │
│ └~──┘ └~──┘ └──┘ │
└∊─────────────────┘
The constant $a contains the uppercase letters A to Z.
Next: Functions and arrays.