Skip to footer navigation.

« Oatmeal

Array programming

How to make mashed potatoes using various different programming paradigms:

Key concepts

In array programming, all data (or most data, perhaps, depending on the system you are using) resides in an array. An array is a rectangular collection of numbers, characters and arrays, arranged along zero or more axes. There are more specific terms for some arrays,

Many array programming languages look different from more commonly seen systems…some array languages even make lisps look normal. The reasons for this are many, and vary from language to language, but the net result is usually similar: once you learn the notation you are able to write very terse programs that are often compact enough to read on a single screen. One reason this is valuable is because you’ve literally reduced the surface area for bugs to rear their heads into.

Array programming languages, similar to forth, simplify the order of operations from normal mathematics, but achieve this by leaving you to think about if a function is either dyadic or monadic:

Data is often thought to have a shape” in array programming.

A scalar (or single number) has no shape, and is typically represented as an empty vector — in APL, this is called a zilde and is represented by the glyph of a 0 with a ~ on top of it, .

A vector’s shape is the number of elements in it, so, the vector 6 7 8 9 has a shape of 4, since that is how many elements comprise it.

Likewise, the shape of matrices is described by their axes, e.g. the matrix

1  2  3  4 
5  6  7  8
9 10 11 12

Has a shape of 3 4 — where 3 is the dimension of the y axis, and 4 of the x axis.

Notice that the shape of a vector has two components, we can inspect the shape of that shape to determine the number of axes of a matrix. Here, that would return 2! This shape-of-shape is also called rank.

Rank is really cool, and is a key concept when thinking through solutions using an array programming paradigm. Array programming languages are usually rank-polymorphic,” meaning that operations magically work with any rank of argument. This one weird trick is really why so many array languages are able to be so terse.

It is important to note that, at least in APL, rank is not depth. A two dimensional array in APL is not a list of lists like it would be in lisp or some other languages. Higher order arrays, like matrices are totally their own thing.

…but what if you want a list of lists? They’re useful and sometimes you’ll wanna reach for one. BOXES!

By enclosing a vector in a box you can interact with it like a scalar. This is magical, and lets you create things that are bigger on the inside than the outside.




Online playgrounds and interpreters