APL Wiki logo



The APL Wiki logo consists of the following numeric matrix, where each number indicates a circle radius: This page explains, step-by-step, how we generate our SVG logo, using the above expression. This demonstrates quite a few APL features.

We will follow APL's evaluation from right to left.

From 1 or from 0?


Whether to count from 0 or from 1 is an old disagreement among programmers. Many APLs let you choose whichever convention you want, but they tend to use 1 by default. To switch convention, we set the variable : By the way, IO stands for Index Origin.

We can already now observe a couple of APL's characteristics…

No reserved words
The name  begins with the special Quad character (a stylised console) which symbolises the computer system itself. APL has no reserved words. Rather, all built-in constants, variables, functions and operators have the prefix  indicating that they are part of the system. Because of this, we call them quad names.

Assignments
Assignment is not done with  like in many other programming languages, but rather with   which also indicates the direction of the assignment: Whatever is on the right gets put into the name on the left.

Generating indices
The  function takes a number N and generates indices until is has made N indices. Since we set  to 0, we count from 0 until right before N:

How many subsets?
Consider a bag with four distinct items. If you stick your hand into the bag and pick two items out, how many different possibilities are there for which pair you get out? $$\{(0,1), (0,2), (0,3), (1,2), (1,3), (2,3)\}$$. APL can tell you this with the Binomial function:

Notice how APL uses traditional mathematical symbols in a generalised way. The traditional post-fix (after its argument) symbol $$!$$ is used with a syntax similar to how you'd normally use $$+$$ or $$\times$$. In fact, all APL functions can be used infix, like $$a-b$$ or prefix, like $$-b$$.

Anyway, how many sets of four could you pick? Obviously, only one; all the items:

Automatic mapping
A really nice feature of APL is its array-orientation. For computations which are defined on single elements (scalar functions), mapping is implicit: (What's up with picking zero out of four items? Since all empty hands are equal, there is exactly one such set — the empty set.)

Order of evaluation
We want to generate the indices using Iota … That didn't work! This is because APL dispenses with traditional mathematics' confusing and inconsistent precedence order, replacing it with a simple right-to-left rule:

Swapping arguments
If the arguments of  were swapped, we wouldn't need that parenthesis. Enter the operator (higher-order function) swap which takes a dyadic function on its left and creates a new derived function which is identical to the original, but has swapped arguments:

A number is a number
The next step is to halve everything: Notice how we were dealing with integers until now, but then we multiply by a float (non-integer). In APL, you don't need to worry about numeric data type conversions. All numeric types get automatically promoted and demoted as needed. APL implementations will usually use the most compact internal representation.

Traditional mathematical symbols
Also notice that we use a proper multiplication symbol, $$\times$$, for multiplication. If traditional mathematics has a symbol for a concept APL includes then APL will use that symbol. Another example is $$\div$$ for division.

Tables
Remember the multiplication table from school? Any function can be made into a table with the Outer Product:

Using an argument twice
It gets tedious to type the same argument twice. Enter the selfie operator which shares its symbol with the above-mentioned swap operator. There's no ambiguity here. Swap swaps the two arguments, while selfie uses a single argument twice: We'll use this in our logo expression:

Rounding
The last step is to round these numbers down. Traditional mathematics writes floor as $$\lfloor x \rfloor$$ but APL is regular, so no function is denoted by two separated symbols. If the function takes a single argument, then the symbol will be on the left, so we write floor as : Those are our radii! Let's save them:

Pairwise summation
Now that we have our radii, we need to figure out where to put our circles. The horizontal pair-wise sum shows how much adjacent circles "reach out" towards each other. N-wise Reduce solves that. Here,  is an operator which takes the plus function and applies it in-between the elements of each horizontal run of length N (the left argument) in the right argument:

Finding maxima
Since the circles line up on a grid, we need the maximum for each horizontal space, that is for each column. APL uses dyadic  as the maximum of a and b. is the columnar maximum-reduction: But obviously, we can't let the circles touch, so we add 1. This gives us all the centre-to-centre distances from the neighbours on the left (or above): We also need an "offset" of the left/top-most circles which don't have any neighbours, so we prepend a 0: Finally, we compute the total offset for each column/row by finding the running total of the offsets: is the identity function, which is just used here get the pass-though value from the assignment, as it would otherwise be hidden. (We call assignment shy.)  is just like   but gives us the intermediate values.

Combining arrays
We need our offsets in two dimensions. So we need to combine the elements of  with themselves in all possible combinations.

The Rank operator allows you to specify what you want paired up with what. In our case, we want individual numbers (which have zero axes) paired up with other individual numbers. As pairing up the numbers in  with those in  : But we want to pair up each of the individual offsets with each of all the offsets. The offsets form a list, so we want to apply this pair-wise pairing function between each number and the entire list, as in: Since we want the offsets paired up with themselves, we can use  again: The offset pairs form our circle locations: ⍴locs←,⍤0⍤0 1⍨offsets 5 5 2 that is, it is a 3D array with 5 layers, 5 rows in each layer, and 2 columns in each row. Each row of our result represents an $$(x,y)$$ value. The first and last layers, which represents the leftmost and rightmost columns of the logo, are: For example, the second row in the first layer is $$(x,y)=(0,9)$$. is the Take function, that is, it takes the first N cells of an array, and a negative value simply means taking from the rear.

Making tags
To help us create our SVG  tags, well set up a couple of helper functions. The first function will help us create tag attributes.

Formatting attributes
APL uses a high minus, to indicate that a number is negative. This avoids confusion with the negate function. However, SVG uses a regular dash, so we need to change our numeric arrays into character representations (using the format function, ), and replace all occurrences of the symbol: The attribute value needs to be quoted, so we prepend two quotation marks, and then we rotate the text one step (left), thereby pushing the first one to the end: Finally, create the full attribute phrase:

Our first function
In the most basic form, a dfn ("dee fun") is just an expression in curly braces with  and   representing the left and right arguments, just like they are the leftmost and rightmost letters of the Greek alphabet: Notice that assignment works for functions too! Remember the automatic mapping? It doesn't apply to user-defined functions, but we can use the Each operator instead:

Flattening enclosed things and enclosing flat things
Next, we make this list of strings (each of which is actually just a character list) into a simple list with the enlist function, and use above same concatenation and rotation techniques to finalise our tag: Let's create a dfn for that too: Notice that a put in ) which encloses the result. This is because I want to deal with these tags as scalar elements. Now we can create our circles (and show the first three: Here we're using  again, but this time only with a single argument, so we only specify one rank — the rank of the sub-arrays we want   called on. Rows are vector (list), so that's rank 1. The  doesn't do anything other than separate   from , so the   knows what is its right operand (which specifies on what sub-arrays the function left operand is to be called) and what is its right argument (which contains the arguments for that function in its sub-arrays).

Monadic  is called Ravel as it unravels an array into a vector, but unlike   it doesn't flatten enclosed elements.

The container
APL Wiki is a MediaWiki which has strict requirements on the dimensions of the site logo: Since the circles on the first row and in the first column are at position 0, we need our SVG to begin a bit further to the top left. Similarly, it needs to end a bit further to the bottom right than the last circle. How much? Well, the maximum radius (which would extend up and to the left) in the first row (and column) is: But let's add a bit more so the circles don't touch the image edge:

Taking things to a higher dimension
Finding the centres of the first and last circles with  but this time with a 2-element left argument (this takes the first 1 layer and the first 1 row of that, etc.), we can find out where to begin, and the size of our image, which makes up the "viewBox" attribute: The extra ravel  is because   and   are matrices, while   needs a vector.

Now we construct the opening tag:

In-place modification through assignment
We can do in-place concatenations to add all the circle tags and the closing tag: You may recognise the pattern here as augmented assignment from C and related programming languages. APL allows you to use any function to modify values in-place. If you are not familiar with this, then just think of  as   (but the pass-though value is whatever is on the right of the assignment arrow).

Doing something with what we made
Now we we can put the SVG data into a native (OS) file: The result is the number of bytes written (which can vary due to line ending standards). Alternatively, flatten the svg and we can show it in an embedded HTML window: ]