Simple examples

This page contains examples that show APL's strengths. The examples require minimal background and have no special dependencies. If these examples are too simple for you, have a look at our advanced examples.

Arithmetic mean
Here is an APL program to calculate the average (arithmetic mean) of a list of numbers, written as a dfn: It is unnamed: the enclosing braces mark it as a function definition. It can be assigned a name for use later, or used anonymously in a more complex expression.

The  refers to the argument of the function, a list (or 1-dimensional array) of numbers. The  denotes the tally function, which returns here the length of (number of elements in) the argument. The divide symbol  has its usual meaning.

The parenthesised  denotes the sum of all the elements of. The  operator combines with the   function: the   fixes the   function between each element of , so that is the same as

Operators
Operators like  can be used to derive new functions not only from primitive functions like , but also from defined functions. For example will transform a list of strings representing words into a comma-separated list: So back to our mean example. gives the sum of the list, which is then divided by, the number elements in it.

Tacit programming
In APL’s tacit definition, no braces are needed to mark the definition of a function: primitive functions just combine in a way that enables us to omit any reference to the function arguments — hence tacit. Here is the same calculation written tacitly:

The operator  can also be used to modify the   function to produce a moving average. or, more verbosely

Text processing
APL represents text as character lists (vectors), making many text operations trivial.

Split text by delimiter
gives 1 for true and 0 for false. It pairs up a single element argument with all the elements of the other arguments: returns its right argument: returns a list of runs as indicated by runs of 1s, leaving out elements indicated by 0s: We use the comparison vector to partition the right argument:

Try it now!

Notice of you can read the tacit function  like an English sentence: The inequality partitions the right argument.

Indices of multiple elements
gives us a mask for elements (characters) in the left argument that are members of the right argument: gives us the indices where true (1): We can combine this into an anonymous infix (dyadic) function:

Frequency of characters in a string
The Outer Product allows for an intuitive way to compute the occurrence of characters at a given location in a string: Then it is simply a matter of performing a sum-reduce  to calculate the total frequency of each character:

Parenthesis nesting level
We can expand on the use of  in the above example to perform more complex calculations. First we compare all characters to the opening and closing characters; An opening increases the current level, while a closing decreases, so we convert this to changes (or deltas) by subtracting the bottom row from the top row: The running sum is what we're looking for:

Grille cypher
A grille is a 500 year old method for encrypting messages. Represent both the grid of letters and the grille as character matrices. Retrieve elements of the grid where there are spaces in the grille. An alternative method using ravel.