My Musical Introduction to APL

Michael Sperone
Sep 3, 2020 · 7 min read

My musical life and computer programming life cross paths frequently. I probably was interested in computers first, but I got seriously into programming later in life when I started doing it for my music. I oscillate back and forth between the two — either focused just on one or the other but often combining the two equally. Recently I’ve been focused on programming, but I found a bridge back to music by way of learning about a new (to me) programming language.

I didn’t know anything about APL when I started looking into it, other than that I’ve seen it referenced in books and articles as one of those “interesting languages to look into.” It’s recommended as a language you might want to study for its unique perspective, as opposed to its practical application in today’s world. APL is the acronym for the self-descriptive and literal name of the language — A Programming Language. It was developed in the 1960s by Kenneth Iverson as a mathematical notation designed specifically for working with arrays and matrices — with the intent to create a programming language using the same notation.

The first thing that caught my attention was the alphabet of symbols. Though most languages include the basic math symbols (+, -, /, *), APL is almost exclusively symbols such as ⌽, ⌿, ÷, ≢, |.

The second thing that caught my attention was that APL uses a matrix as the main data-type. This particularly piqued my interest, since a matrix (and it’s 1-dimensional counterpart: an array) is great for working with groups of musical notes. I had in mind the 20th-century music theory used in serialist music (serialism). Serialism seeks to treat all 12 notes equally by placing them in a particular order and not repeating any notes until all have been played once*. The resulting order of notes is called a “tone row”, and in mathematical terms, we can call it an array. In this system, we represent notes with the numbers from 0 to 11. Each number represents a “pitch class”, or in other words just the note name, and not the octave or register in which it is played.

For example, here is a tone row — shown as notes on a staff, note names, and numbers:

Image for post
Image for post

The numerical relationships translate directly into musical harmonic and melodic relationships. You can apply mathematical operations to that array to produce a number of variations that derive from those. APL is able to easily create these variations because it deals primarily with data represented in the same way.

Setting the stage

Before getting to the variations, I sorted out some basics for handling the 12 note musical system. The first thing I did is assign my tone row to a variable, so I don’t have to keep repeating all 12 numbers every time I reference it. APL uses this assignment operator: ←

apl code: “ROW←0 1 5 11 10 6 2 3 7 9 8 4”
apl code: “ROW←0 1 5 11 10 6 2 3 7 9 8 4”

The next thing I did was handle the modular arithmetic, where the numbers loop back to 0. There is no “12”, because that would be another “A”, which is 0, so 11 + 1 = 0, 10 + 6 = 4, etc… Another way of saying that is this number system is “modulus 12.” Similar to a remainder**, APL calls it the residue and uses the | operator. So that 12 | 16 returns 4. You can also do that with an array, like this:

apl code: “12 | 0 13 17 71 34 30 134 51 7 45 20 124” returns “0 1 5 11 10 6 2 3 7 9 8 4”
apl code: “12 | 0 13 17 71 34 30 134 51 7 45 20 124” returns “0 1 5 11 10 6 2 3 7 9 8 4”

The operation will be applied to each member of the array. This is a feature of the language and works for all APL operations. Which is neat, because most languages I know need a map function to do the same.

The above example returns the tone row we started with. But if you don’t want to manually compare the two, APL uses = to check equality:

APL Operators Doing 12-Tone Operations

In serial music theory, the basic variations of the tone row you learn are retrograde (backwards), inversion (flipped), retrograde inversion (both of those), and transpositions (everything +/- the same).

retrograde — APL has the “reverse” operator: ⌽ It does what you might expect.

apl code: “⌽ ROW” returns “4 8 9 7 3 2 6 10 11 5 1 0”
apl code: “⌽ ROW” returns “4 8 9 7 3 2 6 10 11 5 1 0”


Musical inversion means you travel in the opposite direction, but by the same amount from one number to the next. Instead of starting by going up 1, then up 4, we go down one (which would be 11) then down 4. To invert a 12 tone row, subtract each element from 12. And also apply the residue operator (on the left — using the right-to-left order of operations).

apl code: “12 | 12 — ROW” returns “0 11 7 1 2 6 10 9 5 3 4 8”
apl code: “12 | 12 — ROW” returns “0 11 7 1 2 6 10 9 5 3 4 8”

retrograde inversion — Combines those two.

apl code: “12 | ⌽ 12 — ROW” returns “8 4 3 5 9 10 6 2 1 7 11 0”
apl code: “12 | ⌽ 12 — ROW” returns “8 4 3 5 9 10 6 2 1 7 11 0”

transposition — Add (or subtract) the same number across all 12 notes.

apl code: “12 | ROW + 5” returns “5 6 10 4 3 11 7 8 0 2 1 9”
apl code: “12 | ROW + 5” returns “5 6 10 4 3 11 7 8 0 2 1 9”

The Matrix

From these variations, you can combine them, to fit all versions of the tone row together in one 12 x 12 matrix.

The idea of organizing this into a 12 tone matrix was developed by Arnold Schoenberg, who is often given credit for inventing the 12-tone system. It combines all these forms of the row — 48 altogether — in one simple chart. Each direction is a different form of the tone row, and the rows or columns running in that direction show the different transpositions of that form.

The rows, running left to right, simply list all 12 transpositions of the tone row. And those rows of transpositions (from top to bottom) go in the order of the inversion of the row. So to create this, our APL program could take each number in the inversion, and add a new row to the matrix that transposes the original tone row by that number.

outer product
APL has a combination of symbols that will do this logic. . is the Product operator, and ∘ is the Compose operator. By adding the compose operator before the product, and combine that with ‘x’, you get the expression to calculate the Outer Product: ∘.x Outer product is the same logic I described in the last paragraph, except it multiplies (using the ‘x’) instead of transposes. I replaced the multiplication with addition (∘.+), and it works as I hoped it would.

We use it like this: X ∘.+ Y

We will want to take each element of the inversion, and apply transposition to the prime form:

apl code: (12 — ROW) ∘.+ ROW
apl code: (12 — ROW) ∘.+ ROW

I use parentheses here, so that the result of 12 — ROW counts as the X, instead of just ROW.

And finally, with the residue operator:

apl code: 12 | (12 — ROW) ∘.+ ROW
apl code: 12 | (12 — ROW) ∘.+ ROW

And there you have it. A few short lines of code in APL to assist in planning or expanding a musical idea using serial music techniques.

Summary of Code:

retrograde . . . . . . . . . . ⌽ ROW
inversion . . . . . . . . . . . 12 | 12 — ROW
retrograde inversion . 12 | ⌽ 12 — ROW
transposition . . . . . . . 12 | ROW + 5
matrix . . . . . . . . . . . . . 12 | (12 — ROW) ∘.+ ROW

This barely scratches the surface of APL, but is a good overall introduction and demonstrates how easy it is to deal with multi-dimensional arrays. The result of this simple equation provides the composer with 48 versions of the original tone row to work with. There’s a lot of information presented in a small space, allowing one to view it all at the same time.

In my limited experience — having learned enough to do what I’ve shown here, and having dived a little deeper while writing this — I think this is a wonderful language. You might need a pretty specific set of requirements for APL to be the best choice for your project. There are many other languages and libraries that allow for similar manipulation of arrays and matrices without having to learn this new notation. But I really enjoy seeing code expressed as simple mathematical formulas, and I’m looking forward to trying more advanced things with it.

— —

*This is a gross oversimplification. If you are interested in further details about serialism, see links to more resources below.

**Remainder is not actually the same as modulus — but for the case of positive numbers, they are functionally equivalent.

Links and resources:
APL Introductory video:
APL website:
APL’s terseness explained/defended:

Twelve Tone basics:
U of Puget Sound Serialism:

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store