## Array type & prototype

Any array has a 'type' which is zero for numeric elements and the blank character for character elements. The type of the first element in an array is known as the 'prototype' of the array. The prototype of an array is used in two important areas. Firstly, the prototype of an array is used to determine the structure of an empty array formed from that array. Secondly, the prototype of an array is used as a 'fill' element by those APL functions that can generate extra elements (for example † 'take').

You can see the structure of an array, including its type and prototype, using the ŒDISPLAY system function (or the )DISPLAY system command). In desktop editions of APLX, you can also invoke a Display Window to show array structure, using the pop-up menu which appears when you right-click (or, under MacOS, click-and-hold) over a variable name. See the description of the ŒDISPLAY system function for details of how the structure is shown.

### Empty arrays

An empty array is one in which at least one of the dimensions is zero. For example, an empty vector is a vector of length 0. An array with four rows and zero columns is an empty matrix; it has shape ( 4 by 0), but does not contain any data elements.

A empty vector can most simply be made by one of the expressions shown below. Note that the 'type' of the resultant empty vector can be be numeric or character.

                 ŒDISPLAY ¼0                   ŒDISPLAY ''
           .´.                           .´.
           |0|                           | |
           '~'                           '-'
           empty numeric vector          empty character vector

The alternative expression 0½ is often used, and may again create either a numeric or character empty vector when used with the appropriate argument. (Higher dimensional empty arrays may be made by expressions of the form 0 2½.. and so on - see the entry for ½ 'reshape').

                 ŒDISPLAY 0½676                ŒDISPLAY 0½'PETER'
           .´.                           .´.
           |0|                           | |
           '~'                           '-'
           empty numeric vector          empty character vector

An empty numeric vector can also be created using Ë (Zilde), a primitive constant, which is equivalent to ¼0 or 0½0.

                 X„Ë
                 ½X
           0
                 X¦0½0
           1
                 ŒDISPLAY Ë      © Empty numeric vector
           Ú´Ì
           Û0Û
           À~Ù

### Prototypes of nested arrays

More complex empty arrays result when a nested array is used to generate the empty array. In each case, it is the 'prototype' (derived from the first element of the original array) that dictates the type and structure of the resultant empty array.

                 ŒDISPLAY 'ABC' (¼3)          ŒDISPLAY 0½'ABC' (¼3)
           .…---------------.            .´------.
           | .…--.  .…----. |            | .…--. |
           | |ABC|  |1 2 3| |            | |   | |
           | '---'  '~----' |            | '---' |
           '¹---------------'            '¹------'

The original array (on the left) is a two element nested array whose first element is itself a character vector. The resultant empty vector (the expression on the right) is an empty vector which is itself a nested array with the prototype a length 3 character vector.

                 ŒDISPLAY (2 2½¼4) 'ABC'       ŒDISPLAY 0½(2 2½¼4) 'ABC'
           .…--------------.             .´-------.
           | .…---.  .…--. |             | .…---. |
           | ‡ 1 2|  |ABC| |             | ‡ 0 0| |
           | | 3 4|  '---' |             | | 0 0| |
           | '~---'        |             | '~---' |
           '¹--------------'             '¹-------'

Similar considerations apply above, except that the type of the first element of the original array is numeric, or even, as below, when the first element is mixed.

                 ŒDISPLAY (2 2½ 1 'K' 2 'J' ) (¼4)      ŒDISPLAY 0½(2 2½ 1 'K' 2 'J') (¼4)
           .…-------------------.                 .´--------.
           | .…----.  .…------. |                 | .…----. |
           | ‡ 1 K |  |1 2 3 4| |                 | ‡ 0   | |
           | | 2 J |  '~------' |                 | | 0   | |
           | '+----'            |                 | '+----' |
           '¹-------------------'                 '¹--------'

The prototype concept can be used to display the 'type' of an array. In the example below, the array is first enclosed (›) to form a scalar and an empty vector made from the scalar (0½). Finally the † ('first') function removes the additional level of nesting introduced.

                 ŒDISPLAY VAR
           .…-----------------------------------.
           | .…------------------------.  .…--. |
           | | .…----.  .…-----------. |  |ABC| |
           | | ‡ 1 A |  | .…--.      | |  '---' |
           | | | B 2 |  | |1 2|   7  | |        |
           | | '+----'  | '~--'      | |        |
           | |          '¹-----------' |        |
           | '¹------------------------'        |
           '¹-----------------------------------'

                 ŒDISPLAY †0½›VAR
           .…-----------------------------------.
           | .…------------------------.  .…--. |
           | | .…----.  .…-----------. |  |   | |
           | | ‡ 0   |  | .…--.      | |  '---' |
           | | |   0 |  | |0 0|   0  | |        |
           | | '+----'  | '~--'      | |        |
           | |          '¹-----------' |        |
           | '¹------------------------'        |
           '¹-----------------------------------'

### The prototype as a fill element

Certain functions require the addition of 'fill' elements to arrays, for example the functions † ('take'), \ ('expand') and / ('replicate'). These function can add extra elements to an existing array; the prototype is used to determine the type and shape of the extra elements.

The fill element depends on the type of the array being extended, as follows:

Type of array | Fill Element
Numeric | Zero
Character | Space
Nested or mixed | Prototype or first element, with numbers replaced by zeroes and characters by spaces
Object or Class Reference | The NULL object

                 5†1 2 3                 (fill element for simple numeric is 0)
           1 2 3 0 0
                 ŒDISPLAY 5†'ABC'        (fill element for simple character array
           .…----.                        is blank)
           |ABC  |
           '-----'

                 ŒDISPLAY VAR            (nested array - vector of length 2 )
           .…-----------------------------------.
           | .…------------------------.  .…--. |
           | | .…----.  .…-----------. |  |ABC| |
           | | ‡ 1 A |  | .…--.      | |  '---' |
           | | | B 2 |  | |1 2|   7  | |        |
           | | '+----'  | '~--'      | |        |
           | |          '¹-----------' |        |
           | '¹------------------------'        |
           '¹-----------------------------------'

                 ŒDISPLAY 3†VAR          (prototype used as trailing fill element)
           .…----------------------------------------------------------------.
           | .…------------------------.  .…--.  .…------------------------. |
           | | .…----.  .…-----------. |  |ABC|  | .…----.  .…-----------. | |
           | | ‡ 1 A |  | .…--.      | |  '---'  | ‡ 0   |  | .…--.      | | |
           | | | B 2 |  | |1 2|   7  | |         | |   0 |  | |0 0|   0  | | |
           | | '+----'  | '~--'      | |         | '+----'  | '~--'      | | |
           | |          '¹-----------' |         |          '¹-----------' | |
           | '¹------------------------'         '¹------------------------' |
           '¹----------------------------------------------------------------'

                 ŒDISPLAY ¯3†VAR         (prototype used as leading fill element)
           .…----------------------------------------------------------------.
           | .…------------------------.  .…------------------------.  .…--. |
           | | .…----.  .…-----------. |  | .…----.  .…-----------. |  |ABC| |
           | | ‡ 0   |  | .…--.      | |  | ‡ 1 A |  | .…--.      | |  '---' |
           | | |   0 |  | |0 0|   0  | |  | | B 2 |  | |1 2|   7  | |        |
           | | '+----'  | '~--'      | |  | '+----'  | '~--'      | |        |
           | |          '¹-----------' |  |          '¹-----------' |        |
           | '¹------------------------'  '¹------------------------'        |
           '¹----------------------------------------------------------------'

Topic: APLX Help : Help on APL language : APL Fundamentals : Array type & prototype

[ Previous |Next | Contents | Index ]