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:
| |
Running with Docker
| |
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:
| |
| |
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:
| |
| |
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¯3is the negative number three, while5 - 3is subtraction. This eliminates the ambiguity present in languages where-3could 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:
| |
| |
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:
| |
| |
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⍴xreturns the shape ofx; dyadicn⍴xreshapesxinto shapen- Rank is the number of dimensions;
⍴⍴xreturns the rank ofx(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
Comments
Loading comments...
Leave a Comment