Array programming
How to make mashed potatoes using various different programming paradigms:
- Imperative paradigm, get a potato, wash the potato, peel the potato, boil the potato, mash the potato, place into a bowl. Repeat until you have the desired quantity of mashed potatoes.
- Functional paradigm, collect all your potatoes, peel all your potatoes, boil all your potatoes, mash all your potatoes, place all the mash into a bowl.
- Array paradigm, grow mashed potatoes in the dirt. Extract the mashed potatoes from the earth, and eat them piping hot from the ground.
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,
- a single number is a scalar
- a list is a vector
- two dimensional arrays are matrices
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:
- A dyadic function takes both a left, and a right argument, like addition in grade-school mathematics,
+
and in most programming languages,1 + 1
- A monadic function, on the other hand, is a function that only takes an argument at the right, for instance APL’s factorial,
!7
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.
Resources
Languages
- apl, proprietary version from Dyalog
- gnu apl, open source variant with less-than-ideal tooling
- bqn, my fav of the weirdos, all open source, too!
- kdb+ and q, proprietary, but fast as sin
- lil, love of my life — sorta like “what if Lua but a functional array language”
- j, do you like number theory? J loves it
- julia, not strictly an array language, but it has a lot of useful facilities for array programming in a python-scented environment
- kona, open source implementation of an older version of k
- ivy, from Rob Pike
- i, is much like k — many implementations for many languages
References
- apl cart
- bqn crate
- ngnkcart
- bqn help
- Q for Mortals; An introduction to q programming
- J wiki
- ktye/i, a brief introduction about the peculiarities of ktye/k
- kona wiki, includes links to a lot of other useful resources
- lilt, all about the lil terminal
- learn apl, high-level, but in depth tutorial
- k wiki
Online playgrounds and interpreters
- try apl
- try apl mini, introduces each glyph 1 by 1
- bqn pad
- oK
- vector zoo, the zoo supports a bunch of different array programming languages
- ngn/apl
- kparc