2 min read

APL a Day #2: Arrays, Values

Last session belabored the function, it's relation to APL, and how APL's treatment of functions differs from traditional mathematics. Keep functions in mind: they come first for a reason. All future sessions will assume that you understand or can quickly figure out functions and how they were. Today's session introduces the other critical concept in APL (as seen through the lens of Co-dfns): arrays and values.

Full blown APL as found in something like Dyalog actually has many features surrounding data encoding and its representation. The traditional and most elegant method of data handling in APL, however, centers around the array. In C, numeric values, especially pointers, play a critical role. In Smalltalk, Objects come front and center. In many pure functional languages like Haskell, ADTs and case analysis receives most of the attention. APL, however, marks its territory largely by how well it works with arrays, and how well it allows arrays to work for a wide variety of problems. Other languages have arrays of some sort, often of a more limited extent, but APL adds a good deal of punch to your basic array, allowing arrays to exceed their expected usefulness. Some might argue that APL takes arrays too far, warping its users' sense of reality; these critics have clearly never worked with APL in any serious capacity, the reader may judge whether or not their opinion matters. For the purposes of this series, however, values and arrays are synonymous.

Taking a step back, session #1 only considered methods of manipulating information or values (data of some kind), namely functions, and that very generally. What about data itself? Without values, functions sit idle and useless. Values are information arranged in a fashion suitable for manipulation by APL. In this case, values are arrays. Traditional mathematics introduces the concept of an array very late. All values, including numbers and characters, have an array interpretation, and indeed, all values in APL are arrays. For the technically minded, APL's arrays are finite, rectangular, arbitrary dimension arrays.

Numbers and characters are the smallest sort of array, called scalars. In a sense, they cannot be divided, that is, they are atomic. Arranging numbers and characters in other ways gives different types of arrays. Laying them out in a row, like so, gives a vector:

0 1 2 3 4 5 6 

A vector is an arrangement of values into a single line. That is, it is a one-dimensional layout of values. A matrix is a two dimensional layout, like so:

 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 

Arrays, then, are all about how to arrange various values into rectangular shapes. Since arrays can have any number of positive dimensions, higher-order arrays, called noble arrays, exist. For now, remember that scalars have zero dimensions, a vector has one dimension, and a matrix has two.

A vector of characters, rather than numbers, is a string, like so:

The quick brown fox jumps over the lazy dog. 

An array can have numbers, characters, or even a combination of numbers, character, and other, higher-dimensioned arrays inside of it for values, all arranged according to some pattern, or shape.

Working with data is working with an array that encodes the data into a suitable arrangement for processing. Efficient APLers know how to encode a great many forms of data into sophisticated layouts that enable efficient manipulation.

The next session will cover in more detail how to treat these arrangements more formally, and how to reason about the shapes of arrays.