Chapter 5: Building Arrays
This chapter is about building arrays. First we look at building arrays from lists, and then at joining arrays together in various ways to make larger arrays.
5.1 Building Arrays by Shaping Lists
The dyadic verb $ (dollar) is called "Shape". The expression (x $ y) produces an array of the items of the list y, with shape x, that is, with dimensions given by the list x. For example:
If the list y contains fewer than the number of items needed, then y is re-used in cyclical fashion to make up the number of items needed. This means that an array can be built to show some simple patterning, such as all elements being the same, for example.
Instead of re-using values from y, a value to fill the array can be specified with the "Customize" conjunction (!.) (exclamation-mark dot).
2 3 ($ !. '*') 'ab' ab* ***
The "Shape" verb, dyadic $, has a companion verb, "ShapeOf" (monadic $), which yields the list-of-dimensions, that is, shape, of its argument. To illustrate:
An array can be of length zero in any of its dimensions. If EL is a zero-length, or empty, list then it has no items, and so, after appending an item to it, the result will have one item.
Similarly, if ET is an empty table with no rows, and say, 3 columns, then after adding a row, the result will have one row.
It sometimes occurs that we need to build a scalar. A scalar has no dimensions, that is, its dimension-list is empty. We can give an empty list as the left argument of $ to make a scalar:
(0$0) $ 99 99
We said that (x $ y) produces an x-shaped array of the items of y. If y is a table, then each item of y will itself be a list (a row). Hence in general the shape of (x$y) will be not just x, but rather x followed by the shape of an item of y.
The next sections look at building new arrays by joining together arrays we already have.
5.2 Appending, or Joining End-to-End
Recall that any array can be regarded as a list of items, so that for example the items of a table are its rows. The verb , (comma) is called "Append". The expression (x,y) is a list of the items of x followed by the items of y.
B =: 2 3 $ 'UVWXYZ' b =: 3 $ 'uvw'
In the example of (A,B) above. the items of A are lists of length 3, and so are the items of B. Hence items of A are compatible with, that is, have the same rank and length as items of B. What if they do not? In this case the "Append" verb will helpfully try to stretch one argument to fit the other, by bringing them to the same rank, padding to length, and replicating scalars as necessary. This is shown the following examples.
5.2.1 Bringing To Same Rank
Suppose we want to append a row to a table. For example, consider appending the 3-character list b (above) to the 2 by 3 table A (above) to form a new row.
Notice that we want the two items of A to be followed by the single item of b, but b is not a 1-item affair. We could do it by reshaping b into a 1 by 3 table, that is, by raising the rank of b. However, this is not necessary, because, as we see, the "Append" verb has automatically stretched the low-rank argument into a 1-item array, by supplying leading dimension(s) of 1 as necessary.
5.2.2 Padding To Length
When the items of one argument are shorter than the items of the other, they will be padded out to length. Characters arrays are padded with the blank character, numerical arrays with zero.
5.2.3 Replicating Scalars
A scalar argument of "Append" is replicated as necessary to match the other argument. In the following example, notice how the scalar '*' is replicated, but the vector (1 $ '*') is padded.
5.3 Stitching, or Joining Side-to-Side
The verb ,. (comma dot) is called "Stitch". In the expression (x ,. y) each item of x has the corresponding item of y appended.
5.4 Laminating, or Joining Face-to-Face
The verb ,: (comma colon) is called "Laminate". In the expression (x ,: y), if x and y are, say, two similar tables, then we can imagine the result as one table laid on top of the other to form a 3-dimensional array. Thus the arguments are joined along a new dimension, always of length 2. The result has two items, of which the first is x and the second is y. This means that the new dimension is the first.
The verb ; (semicolon) is called "Link". It is convenient for building lists of boxes.
Notice how the example of 5;12;1995 shows that (x;y) is not invariably just (< x),(< y) . Since "Link" is intended for building lists of boxes, it recognises when its right argument is already a list of boxes. If we define a verb which does produce (< x),(< y)
foo =: 4 : '(< x.) , (< y.)'
we can compare these two:
5.6 Unbuilding Arrays
We have looked at four dyadic verbs: "Append" (,), "Stitch" (,.), "Laminate" (,:) and "Link" (;). Each of these has a monadic case, which we now look at.
Monadic ; is called "Raze". It unboxes elements of the argument and assembles them into a list.
Monadic , is called "Ravel". It assembles elements of the argument into a list.
5.6.3 Ravelling Items
Monadic ,. is called "Ravel Items". It separately ravels each item of the argument to form a table.
"Ravel Items" is useful for making a 1-column table out of a list.
Monadic ,: makes a 1-item array out of any array, by adding a leading dimension of 1.
5.7 Arrays Large and Small
For small arrays, where the contents can be listed on a single line, there are alternatives to using $, which avoid the need to give the dimensions explicitly.
To build large tables, a convenient method is as follows. First, here is a "utility" verb (that is, a verb which is useful for present purposes, but we don't need to study its definition now.)
ArrayMaker =: ". ;. _2
The purpose of ArrayMaker is to build a numeric table row by row from the lines of a script.
table =: ArrayMaker 0 : 0 1 2 3 4 5 6 7 8 9 )
(See Chapter 17 for an explanation of how ArrayMaker works). Arrays of boxes can also be entered from a script in the same way:
X =: ArrayMaker 0 : 0 'hello' ; 1 2 3 ; 8 'Waldo' ; 4 5 6 ; 9 )
We have reached the end of Chapter 5.
Table of Contents
Copyright © Roger Stokes 2000. This material may be freely reproduced, provided that this copyright notice and provision is also reproduced.
last updated 17Mar00