💾 Archived View for tranarchy.fish › ~autumn › apl2 › appendixF.gmi captured on 2023-04-26 at 13:25:47. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-04-19)
-=-=-=-=-=-=-
<div class="chapter-rule">
<hr class="chapter-long">
<p>Appendix</p>
<hr class="chapter-short">
<div>
<div>
F
</div>
</div>
</div>
<h2 id="apl2-release-3-functions">APL2 Release 3 Functions</h2>
<p>In November 1987, <span class="small-caps">IBM</span> announced <span class="small-caps">APL2</span> Release 3 which contains two new primitive functions:</p>
<ul>
<li><strong>indexing</strong></li>
<li><strong>partition</strong></li>
</ul>
<p>This appendix briefly describes these functions. For further information on these functions and the other features of <span class="small-caps">APL2</span> Release 3. see <em>“APL2 Programming: Language Reference”</em> (<span class="small-caps">IBM</span> order number SH20-9227).</p>
<h4 id="indexing">Indexing<a href="#indexing" class="section-link">§</a></h4>
<p>The <strong>indexing</strong> (<code>⌷</code>) function is like indexing with brackets except that it has the syntax of a dyadic function and can be used with operators. In general terms, indices that would be listed in square brackets separated by semicolons are instead written as the left argument of the <strong>indexing</strong> function. The right argument is the array being indexed:</p>
<blockquote>
<pre> A[I;J;K]</pre>
<p>is written as</p>
<pre> I J K ⌷ A</pre>
</blockquote>
<p>The length of the vector written on the left of the <strong>indexing</strong> function must match the number of axes in the array being indexed. Thus to index a rank-3 array, you need a three-item left argument. For example:</p>
<pre> B←2 3 4⍴⍳24
B
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
B[2 1;1;3 1]
15 13
3 1
(2 1) 1 (3 1) ⌷ B
15 13
3 1</pre>
<p>Because this <strong>indexing</strong> function can be used with operators, you can apply several indices to one array independently:</p>
<pre> (1 1 1)(2 1 3)⌷¨⊂B
1 15</pre>
<p>This is sometimes called <em>scatter indexing</em> because you can use it to select arbitrary items from an array as opposed to rectangular subsections.</p>
<p>You can index each of a set of arrays with the same index as follows:</p>
<pre> (⊂(2 1) 1 (3 1)) ⌷¨ B (10×B) (100×B)
15 13 150 130 1500 1300
3 1 30 10 300 100</pre>
<p>When indexing a vector, a scalar left argument is treated as though it were a vector of length 1:</p>
<pre> V←'ABCDEFGHI'
3⌷V
C</pre>
<p>Be careful when you want to select more than one item from a vector. For example, to select the third and fourth item from the character vector <code>V</code>, you might try the following expression:</p>
<pre> 3 4⌷V
RANK ERROR
3 4⌷V
^ ^
→</pre>
<p>This expression fails because a two-item left argument can only index a rank-2 array. A nested left argument does the desired operation:</p>
<pre> (⊂3 4)⌷V
CD</pre>
<p>The <strong>indexing</strong> function may be used with an axis to indicate elided indices. The following examples contrast indexing with brackets to the <strong>indexing</strong> function:</p>
<pre> B←2 3 4⍴⍳24
B
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
B[2 1;;3 1]
15 13
19 17
23 21
3 1
7 5
11 9
(2 1) (3 1) ⌷[1 3] B
15 13
19 17
23 21
3 1
7 5
11 9</pre>
<h4 id="partition">Partition<a href="#partition" class="section-link">§</a></h4>
<p>The purpose of the <strong>partition</strong> (<code>⊂</code>) function is to divide up the right argument producing one item in the result for each division. For example, given the vector <code>'JIM,JOHN,GEORGE,FRED'</code>, the <strong>partition</strong> function may be used to produce a vector of the four names as follows:</p>
<pre> LIST←'JIM,JOHN,GEORGE,FRED'
M←(LIST≠',')
M
1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1
M⊂LIST
JIM JOHN GEORGE FRED
DISPLAY M⊂LIST
.→-----------------------------.
| .→--. .→---. .→-----. .→---. |
| |JIM| |JOHN| |GEORGE| |FRED| |
| '---' '----' '------' '----' |
'∊-----------------------------'</pre>
<p>A division is expressed in the left argument as an increase in value. Whenever an item of the left argument is greater than the previous item, a new division is started. Any item in the right argument corresponding to a zero in the left argument does not appear in the result. The magnitude of items in the left argument has no influence on the operation. Only increases in value are relevant:</p>
<pre> DISPLAY (2×M)⊂LIST
.→-----------------------------.
| .→--. .→---. .→-----. .→---. |
| |JIM| |JOHN| |GEORGE| |FRED| |
| '---' '----' '------' '----' |
'∊-----------------------------'</pre>
<p>If the left argument does not contain any zeros, then all items from the right argument appear in the result:</p>
<pre> DISPLAY (1+M)⊂LIST
.→---------------------------------.
| .→---. .→----. .→------. .→----. |
| |JIM,| |JOHN,| |GEORGE,| |FRED,| |
| '----' '-----' '-------' '-----' |
'∊---------------------------------'</pre>
<p>A good way to remember that an increase in the left argument causes the start of a new item in the result is to remember that an increasing arithmetic progression results in a vector of one-item vectors:</p>
<pre> (⍳5)⊂'ABCDE'
A B C D E
DISPLAY (⍳5)⊂'ABCDE'
.→--------------------.
| .→. .→. .→. .→. .→. |
| |A| |B| |C| |D| |E| |
| '-' '-' '-' '-' '-' |
'∊--------------------'</pre>