Beginner

Variables and Types in APL

Learn how APL handles variables, arrays, and data types — from scalars and vectors to matrices and character data

In most languages, variables hold a single value. In APL, every variable holds an array — and that changes everything. A scalar (single number) is just an array of rank 0. A string is a character vector. A matrix is a rank-2 array. APL’s entire type system is built around this unified array model.

Assignment in APL uses the left arrow , which you can read as “gets” or “is assigned”. This single operator is how all variables are created and updated, regardless of the data they hold.

APL’s type system is dynamic and strong: you don’t declare types, but APL knows whether something is numeric or character data and won’t silently mix them in ways that produce nonsense. The shape and rank of an array are always known and queryable.

In this tutorial you’ll learn how to assign variables, understand the difference between scalars, vectors, and matrices, work with character and numeric data, and use APL’s built-in introspection functions to inspect your data.

Variable Assignment with

The left arrow is APL’s only assignment operator. There are no var, let, int, or String keywords — just name, arrow, value.

Create a file named variables_basic.apl:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
⍝ Scalar assignments — rank-0 arrays
n  42
pi  3.14159
flag  1       ⍝ APL uses 1 for true, 0 for false

⍝ Character vector (string)
name  'APL'

⍝ Numeric vector — a rank-1 array
scores  95 87 72 100 61

⍝ Display variables
n
pi
flag
name
scores
)OFF

Running with Docker

1
2
3
4
5
# Pull the GNU APL image
docker pull juergensauermann/gnu-apl-1.8:latest

# Run the basic variables example
docker run --rm -v $(pwd):/app -w /app juergensauermann/gnu-apl-1.8 apl --silent --noColor --noCIN -f variables_basic.apl

Expected Output

42
3.14159
1
APL
95 87 72 100 61

Arrays: Vectors and Matrices

APL’s true power shows when you move from scalars to multi-dimensional arrays. The shape operator (rho) both queries and creates arrays.

Create a file named variables_arrays.apl:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
⍝ A vector of integers (rank 1)
v  10 20 30 40 50

⍝ Create a 3×3 matrix using reshape (⍴)
m  3 31 2 3 4 5 6 7 8 9

⍝ A character matrix — each row is a word padded to same length
words  3 5'APL  FORTRAN'

⍝ Query shape with ⍴ (monadic use)
⍝ For a vector, shape is its length
'Shape of v:'
v

⍝ For a matrix, shape is rows and columns
'Shape of m:'
m

⍝ Rank is the number of dimensions — length of shape
'Rank of v (⍴⍴v):'
⍴⍴v

'Rank of m (⍴⍴m):'
⍴⍴m

⍝ Display the matrix
'The 3×3 matrix:'
m
)OFF
1
2
docker pull juergensauermann/gnu-apl-1.8:latest
docker run --rm -v $(pwd):/app -w /app juergensauermann/gnu-apl-1.8 apl --silent --noColor --noCIN -f variables_arrays.apl

Expected Output

Shape of v:
5
Shape of m:
3 3
Rank of v (⍴⍴v):
1
Rank of m (⍴⍴m):
2
The 3×3 matrix:
1 2 3
4 5 6
7 8 9

Numeric Types and Precision

APL supports integers, floating-point numbers, and complex numbers. GNU APL uses double-precision floating point for all real numbers. There is no separate integer type — APL handles the distinction internally.

Create a file named variables_numeric.apl:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
⍝ Integer and float — both are numeric arrays
i  7
f  2.718281828

⍝ Large integer
big  1000000

⍝ Negative numbers use the high minus ¯ (not the subtraction minus)
neg  ¯42
negfloat  ¯3.14

⍝ A vector mixing what looks like ints and floats
⍝ APL treats them all as numeric
mixed  1 2.5 3 4.7 5

⍝ Arithmetic works element-by-element on the whole array
doubled  2 × mixed

'Original:'
mixed

'Doubled:'
doubled

'Negative integer:'
neg

'Negative float:'
negfloat
)OFF
1
2
docker pull juergensauermann/gnu-apl-1.8:latest
docker run --rm -v $(pwd):/app -w /app juergensauermann/gnu-apl-1.8 apl --silent --noColor --noCIN -f variables_numeric.apl

Expected Output

Original:
1 2.5 3 4.7 5
Doubled:
2 5 6 9.4 10
Negative integer:
¯42
Negative float:
¯3.14

Note on the high minus: APL uses ¯ (Unicode U+00AF, macron) as the negative sign for literal values, keeping it distinct from the - subtraction operator. So ¯3 is the negative number three, while 5 - 3 is subtraction. This eliminates the ambiguity present in languages where -3 could mean either.

Character Vectors and Type Checking

In APL, strings are character vectors — rank-1 arrays of characters. APL distinguishes sharply between numeric and character data. The ceiling operator works on numbers; applying it to characters is a type error.

Create a file named variables_types.apl:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
⍝ Character vector (string)
greeting  'Hello, APL'

⍝ Single character — still a vector of length 1 when quoted
⍝ A true scalar character can be created with ⊃ (first/disclose)
ch  'X'

⍝ Numeric vector
nums  3 1 4 1 5 9 2 6

⍝ The ≡ (depth) function reveals nesting level
⍝ For simple arrays it returns 0 (non-nested) or 1 (nested)

⍝ Check the length (number of elements) with ⍴
'Length of greeting:'
greeting

'Length of nums:'
nums

⍝ Index into a character vector (APL uses 1-based indexing by default)
'First character of greeting:'
greeting[1]

'Characters 1 through 5:'
greeting[1 2 3 4 5]

⍝ Concatenate character vectors with ,
full  greeting, '!'
'Concatenated:'
full

⍝ Check if something is numeric: 0=numeric, non-zero=not numeric in context of ⌈⌊
⍝ Use the APL idiom: 0=⊃0⍴⍬⊣nums to check numeric —
⍝ simpler: just show that math works on nums, not on greeting
'Sum of nums:'
+/nums
)OFF
1
2
docker pull juergensauermann/gnu-apl-1.8:latest
docker run --rm -v $(pwd):/app -w /app juergensauermann/gnu-apl-1.8 apl --silent --noColor --noCIN -f variables_types.apl

Expected Output

Length of greeting:
10
Length of nums:
8
First character of greeting:
H
Characters 1 through 5:
Hello
Concatenated:
Hello, APL!
Sum of nums:
31

Boolean Values: 0 and 1

APL has no separate boolean type. Comparison operators return numeric arrays of 0s and 1s. This integrates naturally with arithmetic — you can sum a boolean vector to count how many elements satisfy a condition.

Create a file named variables_boolean.apl:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
⍝ APL comparisons return 0 or 1
a  5
b  3

'a > b:'
a > b

'a = b:'
a = b

'a < b:'
a < b

⍝ Comparisons on arrays return boolean arrays element-by-element
prices  10 25 8 42 15 3

'Prices above 12:'
prices > 12

'Count of prices above 12:'
+/ prices > 12

⍝ Boolean indexing — select elements where condition is true
'Which prices are above 12 (the values):'
(prices > 12) / prices
)OFF
1
2
docker pull juergensauermann/gnu-apl-1.8:latest
docker run --rm -v $(pwd):/app -w /app juergensauermann/gnu-apl-1.8 apl --silent --noColor --noCIN -f variables_boolean.apl

Expected Output

a > b:
1
a = b:
0
a < b:
0
Prices above 12:
0 1 0 1 1 0
Count of prices above 12:
3
Which prices are above 12 (the values):
25 42 15

Key Concepts

  • is the only assignment operator — used for all variable creation and updates regardless of type
  • Every variable is an array — scalars are rank-0 arrays, strings are character vectors, matrices are rank-2 arrays
  • (rho) is dual-purpose: monadic ⍴x returns the shape of x; dyadic n⍴x reshapes x into shape n
  • Rank is the number of dimensions; ⍴⍴x returns the rank of x (the length of its shape)
  • High minus ¯ prefixes negative number literals — it is not the subtraction operator
  • No separate boolean type — comparisons return 0/1 integer arrays that compose naturally with arithmetic
  • 1-based indexing — APL arrays are indexed starting at 1 by default (this can be changed with ⎕IO←0)
  • Numeric and character data are distinct — character vectors use single quotes, and arithmetic operators do not apply to character data

Running Today

All examples can be run using Docker:

docker pull juergensauermann/gnu-apl-1.8:latest
Last updated:

Comments

Loading comments...

Leave a Comment

2000 characters remaining