Looking at Arrays

In good representations, similar things will look alike and distinct things will look different. In great representations, good representations combine with metaphor to create meaning. The metaphor used in J is the array. If you are aware of J’s approach to rank, shape, or item then skip ahead to the next blog entry, otherwise read on.

Row of three lego blocks

Here we have three blocks in a row. This array has three items and its shape is 3.

Using the J text representation we’d show it this way:

   A3=: 3 $ 5 6 7

where $ is the dyadic verb “Shape” that takes the right argument and makes it into the shape specified by the left argument. The =: is the assignment operator that assigns the result to the variable we have called A3:


5 6 7

We have A3 representing three numbers in an array (list) of shape 3. J can tell us this using the monadic form of $ known as “Shape Of”:

   $ A3


Two rows of three lego blocks

This one has two rows of three blocks suggesting that the shape is 2 3. We could also think of this array as two items, where each item is a row of three blocks. The same array in J would look like this:

   A23=: 2 3 $ 5 6 7 8 9 10
5 6 7
8 9 10
   $ A23

2 3

In J is the number of items in an array is the first item in the shape (yes the shape is an array as well and it has items). J uses the monadic verb “Tally” (#) to return the number of items in an array:

   $ A3

   # A3

   $ A23

2 3
   # A23


A big difference between the shapes of A3 and A23 is that the shape of A3 has 1 item and the shape of A23 has 2 items. This idea of the number of items in the shape is interesting enough that we give it its own name: rank. So A3 has rank 1 and A23 has rank 2. In J we can find this by using the monadic verb # “Tally” to find the number of items in the shape, like this:

   # $ A3

   # $ A23


Two planes each with two rows of three lego blocks

Above, we have a brick of 2 tables, each table with 2 lists, and each list with length 3. Using J we get:

   A223=: 2 2 3 $ 5 6 7 8 9 10 11 12 13 14 15 16
5  6  7
8  9 10

11 12 13

14 15 16


   $ A223

2 2 3
   # $ a223


Using the empty line to separate each item in the array is a particularly clever approach that J uses to display higher rank arrays. In fact, J adds an extra line as the rank increases, so that the items in rank 4 arrays are separated by 2 empty lines.

To sum up: (1) arrays have shapes, (2) the first number in the shape is the number of items, (3) the rank of an array is the length of its shape. If this isn’t clear, go back and review the examples above or take a look at Chapter 5 in Henry Rich’s excellent “J for C Programmers”. In the next blog entry we’ll explore why the J representation for arrays is not as clear as it could be.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s