Let's Learn A Programming Language (APL) - Part 2
It's time for more APL! In Part 1, we took an elementary overview of what APL is and how to do basic arithmetic in it. Today we’ll learn some technical terms, some more new functions and variables.
So, let’s start.
First we’ll look at few common names of different “shapes” of data in APL:
- Scalar: Scalar referes to asingle value. for example the number
12
or the letter'A'
- Vector: Vector is a simple linear list of values. For example, space separated numbers
1 2 3
or string of letters within single quotes—'ANIKET'
- Matrix: It’s a list with two dimensions. Also commonly called “Table”
- Array: A list of values, no matter what it’s dimension is.
- Cube: Three dimensional array.
Keeping these in mind, let’s take a look at three new symbols:
First one is ⍴
. If you’re using the keyboard layout from the previous day, you can get it by Right Alt + R
When used before an array, it returns it’s size
⍴ 1 2 3
3
Easy, right?
Now you might remember that you have seen the functions (we’ll use the word function) in part 1 behave differently based on where they’re placed. Generally functions can have two uses —
- data <function> data
- <function> data
In the first use, the function is “sandwitched” between two arguments. This is called dyadic use.
In the second form the function takes only one data. It’s called monadic usage.
In the example of ⍴
you saw the monadic usage. In the dyadic form, ⍴
is used to arrange data in a shape, or more generally, create matrices. It’s known as reshape.
The first argument of ⍴
is the shape which is an array and the second argument is the data which is to be reshaped. For now, we’ll stick to creating matrices, so the first argument will be an array of length 2.
3 4 ⍴ 1
1 1 1 1
1 1 1 1
1 1 1 1
As you can see, the first element of the shape ( 3
) denotes how many rows should be there, and the second element ( 4
) denotes how many columns should be there.
A 3 × 4 matrix has 12 elements but we have passed only one value in the second argument, So that single value has been repeated to create the matrix. Let’s pass in different number of values.
3 4 ⍴ 1 2 3
1 2 3 1
2 3 1 2
3 1 2 3
If we pass more than one values but less than equal to the number of total values, it will start filling up the matrix from the first row, and if the data finishes, it will start repeating the data from the beginning.
This property can be used to create some useful shapes. For example, to create the identity matrix of order 4, we’d do
4 4 ⍴ 1 0 0 0 0
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
So, if we pass exactly 12 values, nothing will repeat
3 4 ⍴ 1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4
5 6 7 8
9 10 11 12
And if we pass more than 12 values
3 4 ⍴ 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4
5 6 7 8
9 10 11 12
the extras will be ignored.
All the above shapes were two dimensional. But a shape can have more than 2 dimensions. For example, a 3 dimensional array of shape 3 3 3 will be an array of 3 elements. Each element will itself be an array of 3 rows and 3 columns.
3 3 3 ⍴ 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
Here the blank lines separate the subarrays.
Similar is the case of higher dimensions. An array of 4 dimension with shape 3 3 3 3 will be an array of 3 elements, where each element will be an array of shape 3 3 3.
That finishes ⍴
for now. On to the next symbol!
Next one is ⌈
(maximum). Use Right Alt + S to get it.
As evident from the name, in the dyadic usage, it finds maxium of two values
5 ⌈ 12
12
If you pass two arrays, it will do an elementwise comparison and return an array with the maximum elements from both arrays
1 20 59 3 ⌈ 5 10 18 20
5 20 59 20
Here, it checks the first element of both the arrays, and takes the maximum, then takes the second element of both the arrays, and takes the maximum and so on.
In monadic usage, ⌈ works as the “ceiling” function. Meaning it returns the least integer greater than that or equal to its argument. If passed in an array, it does the same thing for each element.
⌈ 4.5
5
⌈ 4.5 3.4 ¯6.5
5 4 ¯6
Just like ⌈
another one is ⌊
(minimum) (Alt + D). ⌊
finds the minimum of two values in dyadic form. If passed in an array, it does elementwise comparison. And in monadic form it works as “floor” function (returns the greatest integer less than or equal to the argument). You can think of it as the opposite of ⌈
5 ⌊ 12
5
1 20 59 3 ⌊ 5 10 18 20
1 10 18 3
⌊ 4.5
4
⌊ 4.5 3.4 ¯6.5
4 3 ¯7
Variables
As in other programming languages, it’s possible in APL to create a variable and use them. We use the assignment operator ←
(Alt + [) to create a variable. The structure for assigning a variable is
variable_name ← value
For example, if we wanted to create a variable named “Aniket” with value 19, we’d do
Aniket ← 19
Remember that, APL will not give any output. since assignment is not an expression. To verify that it is assigned —
Aniket
19
You can do all the arithmetics you have learned with variables
Aniket + 1
20
Aniket × 2
38
÷ Aniket
0.05263157895
You can also assign the result of an operation to a variable. For example, this assigns the minimum of 5 and 29 to the variable a —
a ← 5 ⌊ 29
a
5
Remember these points while working with a variable —
- Variable names are case sensitive. So
ANIKET
,aniket
andAniket
are all different variables. - Variable names must contain only letters, and digits (and some accented letters).
- Variable names can have
_
,∆
(Alt + H) and⍙
(Alt+ Shift + .) - Variable names cannot start with a digit.
That is all for today. In the next part, we’ll see some more functions and operators. We’re heading towards the best parts of APL. Stay tuned.