APLX Help : Help on APL language : APL Fundamentals : Array type & prototype
|
|||||||||||
![]() |
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 You can see the structure of an array, including its type and prototype, using the Empty arraysAn 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 ⎕DISPLAY 0⍴676 ⎕DISPLAY 0⍴'PETER' .⊖. .⊖. |0| | | '~' '-' empty numeric vector empty character vector An empty numeric vector can also be created using X←⍬ ⍴X 0 X≡0⍴0 1 ⎕DISPLAY ⍬ ⍝ Empty numeric vector ┌⊖┐ │0│ └~┘ Prototypes of nested arraysMore 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 ⎕DISPLAY VAR .→-----------------------------------. | .→------------------------. .→--. | | | .→----. .→-----------. | |ABC| | | | ↓ 1 A | | .→--. | | '---' | | | | B 2 | | |1 2| 7 | | | | | '+----' | '~--' | | | | | '∊-----------' | | | '∊------------------------' | '∊-----------------------------------' ⎕DISPLAY ↑0⍴⊂VAR .→-----------------------------------. | .→------------------------. .→--. | | | .→----. .→-----------. | | | | | | ↓ 0 | | .→--. | | '---' | | | | 0 | | |0 0| 0 | | | | | '+----' | '~--' | | | | | '∊-----------' | | | '∊------------------------' | '∊-----------------------------------' The prototype as a fill elementCertain functions require the addition of 'fill' elements to arrays, for example the functions The fill element depends on the type of the array being extended, as follows:
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 | | | | | '+----' | '~--' | | | '+----' | '~--' | | | | | '∊-----------' | | '∊-----------' | | | '∊------------------------' '∊------------------------' | '∊----------------------------------------------------------------' |
|||||||||||
APLX Help : Help on APL language : APL Fundamentals : Array type & prototype
|
Copyright © 1996-2010 MicroAPL Ltd