New Symbols
Human intelligence is not confined to four or five basic operations, though that is truly the limit imposed by the majority of, even the most modern, programming languages.
The inventive creator of APL, Kenneth E. Iverson, therefore added to the set of all the usual symbols a number of new ones:
The symbol for the function maximum returns the greater of two numbers, or of two arrays of numbers compared item by item. There is also, as one might expect, a symbol for minimum.
75.6 ⌈ 87.3
87.3
11 28 52 14 ⌈ 30 10 50 20
30 28 52 20
The function Minimum works the same way:
11 28 52 14 ⌊ 20
11 20 20 14
APL supports about 70 symbols. Consequent upon certain symbols being defined in two ways one could argue at length about the exact number.
That is nothing to worry about: some of the symbols are familiar (such as × or ÷ or again + and -, but also ! and a good many others).
Double Meaning of Symbols
This is not a peculiarity of APL; within algebra we are familiar with the use of symbols as common as the minus sign being used in two ways.
In the expression a = x-y it means the operation subtraction, whereas in a = -y it is the result of the inverse operation, which changes the sign of the number.
The first form is called the dyadic use of the symbol. The second form is called the monadic use of the symbol.
It is the same in APL, where most of the symbols can have two meanings.
For example, to find the shape (the dimensions) of an object, one uses the Greek letter ⍴ (rho), which can be read “shape of”, in its monadic use.
⍴ Price ⍝ Monadic: Price comprises 5 items
5
⍴ Forecast ⍝ Monadic: Forecast comprises 4 rows of 6 items
4 6
Used dyadically, the same symbol will organise values into the specified shape.
2 3 ⍴ 1 2 3 4 5 6
1 2 3
4 5 6
For example, to create the table below requires two pieces of information:
25 60
33 47
11 44
53 28
first the shape to give to the table: 4 2 (4 rows of 2 columns), and next the contents of the table: 25 60 33 47 11 etc.
It is the symbol rho which makes the connection:
Tab ← 4 2 ⍴ 25 60 33 47 11 44 53 28
A new variable Tab is thus created, and this is also how the variables Forecast and Actual above were made.
Conventions
In APL, one gives the name vector to a list of values; which may be composed of numbers like Price and Qty, or of letters like 'Once upon a time'. One calls a table of two dimensions, like Forecast or Tab, a matrix, and a single value, a number like 456.18 or a single letter like 'Q', a scalar.
Reduction: a Linking Notation
Remember that we calculated some costs: 10.4 11.5 10.8 24 16.9 .
So what must we do to work out the total? Mathematicians are creative people who long ago devised the symbol , always with a pretty collection of indices above and below, which is not very compatible with elementary text processing, which must put symbols on a single line.
In APL, the operation is written thus:
+/ Costs
73.6
Simple isn’t it? What’s the point of the indices of the first and last items? This gives the total of all the items of the data without mentioning them!
One speaks of the plus reduction of the variable Costs. To gain a better understanding of the process:
When we write an instruction such as +/ 21 45 18 27 11
it is as if we wrote 21 + 45 + 18 + 27 + 11
to obtain the sum 122
in fact as if we had inserted the symbol + between the values.
But then, if we write ×/ 21 45 18 27 11
it is as if we had written 21 × 45 × 18 × 27 × 11
and obtained the product 5051970
Similarly, if we write ⌈/ 21 45 18 27 11
it is as if we wrote 21 ⌈ 45 ⌈ 18 ⌈ 27 ⌈ 11
and so had obtained the largest item 45
Reduction belongs to a special category of symbols called operators.
The other symbols (+ - × ⌈ > ⍴…) represent functions (addition, subtraction … maximum … shape, etc.).
The arguments of a function are data: Price × Qty
Whereas the left argument of an operator is a function: +/ Costs
(A more rigorous definition is beyond the simple framework given here.)
We may say that reduction allows as many different operations to be carried out as there are symbols for functions (or program names!) to its left: it is an idea of great generality.
Just think: in fact in mathematics we invented for the sum, for the product, min and max for the minimum or the maximum, and still more notable inf (lower bound) and sup (upper bound)!
In APL, the sole symbol / suffices to regularise all this notation!
APL contains six mathematical operators in its most basic versions, and nine in the version of Dyalog APL which is used for this document.
First Program
We want to calculate the average of the following numbers:
Val ← 22 37 41 19 54 11 34
We must divide one expression by another:
first for the sum of the values: +/Val which gives 218
next for the number of values: ⍴Val which gives 7
The calculation can be written as the single formula: (+/Val)÷(⍴Val)
As it is quite likely we shall often want to make this sort of calculation, it is preferable to store this sequence in the form of a program.
In APL we prefer the name defined function to the name program.
Defined functions are produced by a feature of the language which builds them so that they may be used in the same way as the symbols (+ - × ⌈ > ⍴…) which are called primitive functions.
It is outside the scope of this document to explain how to define such a program: having said which, it will look something like the following:
∇ R ← Average V
[1] R ← (+/V)÷(⍴V)
∇
Average is the program name.
V represents the list of values which are defined as the right argument.
R represents the result of the calculation, which will be returned at the end.
The typographical sign ∇ (called del) marks the beginning and end of the printed form of the program.
Once defined, this function may be invoked in a very simple way:
Average Val
31.1428571428
Average 12 74 56 23
41.25
One may thereafter include it in a more complex expression:
10×Average 12 74 56 23
412.5
Share with your friends: |