An Introduction To APL - Prager, William
An Introduction To APL - Prager, William
WILLIAM PRAGER
Digitized by the Internet Archive
in 2012
http://archive.org/details/introductiontoapOOprag
An Introduction to APL
This book is part of
the Allyn and Bacon series
Consulting Editor
PETER WEGNER
Brown University
An Introduction to APL
WILLIAM PRAGER
University Professor of Engineering
and Applied Mathematics
Brown University
Preface vii
1.1 Keyboard 1
1.2 Signing On 2
1.3 Signing Off 4
1.4 Correction of Typing Errors 5
1.5 Evaluation of Arithmetic Expressions 5
Arrays (part 1) 13
2.1 Multiple Evaluations of an Expression 13
2.2 Forming and Indexing of Arrays 16
Operator p. Operator I. Indexing.
2.3 Arithmetic Operations on Arrays 23
Monadic Operators. Dyadic Operators. Special
Operators.
2.4 Boolean Expressions 26
Additional Operators 55
5.1 Dyadic Use of •, !, I , and O 55
5.2 Operators L, I", and ? 56
5.3 Operators ~, A, v, * , and * 60
5.4 Basic Operators 61
Arrays (part 2) 63
6.1 Arrays of Higher Dimensionality 63
6.2 Special Operators 67
Reduction. Inner Product. Outer Product.
Grade Up and Grade Down. Take and Drop.
Decode and Encode. Transpose.
Compression and Expansion.
Character Manipulation 85
7.1 Character Data 85
7.2 Operations on Character Data 89
7.3 Sorting, Coding, Decoding, and Translating 94
References 129
Index 131
Preface
This book is an introductory text for classroom use or independent study, not
a reference manual. It consists of two parts comprising Chapters 1 through 4
and Chapters 5 through 9. After reworking the examples of the first part at
the terminal, the reader should be reasonably proficient in applying APL to the
solution of a wide variety of numerical problems. Study of the second part
will extend his knowledge of the language and enable him to handle nonnumerical
problems as well. At this stage, the reader should try to improve some of the
function definitions in the text. This will be possible because these definitions
were written with an emphasis on transparency rather than elegance. Finally,
to test his ability to follow complex function definitions, the reader may call
There remains the pleasant task of thanking all those who, in various ways,
contributed to the publication of this book: Dr. K. E. Iverson of the Thomas
J. Watson Research Center of IBM for defining APL, and IBM for implementing
the language; Professor Peter Wegner of Brown University for the invitation to
write the book for the series; Professors C. Mylonas and R. A. Vitaleof Brown
University and Dipl. Ing. W. Munzner of the IBM Research Laboratory at Zurich
for their numerous helpful comments; Mrs. D. Archambault for the most careful
preparation of the manuscript; and the editorial staff of the publisher for their
splendid cooperation.
William Prager
Providence, Rhode Island
VII
List of APL Symbols
The order of symbols in this list corresponds to their arrangement on the key-
board, starting with the top row of keys and going from left to right. Overstruck
and composite symbols are listed last. The letters d and D stand for basic dyadic
operators.
* not equal to
v or
A and
minus (as arithmetic operator)
+ plus
^7 grade down
A grade up
! number of combinations (dyadic); factorial (monadic)
ft comment
I system information
dI reduction with respect to last index
1
1.1 KEYBOARD
The APL characters appear on the front rather than the top face of
each key. Each key carries two characters. To type the lower one, it
is sufficient to hit the key, but if the upper character is to be typed,
the shift key must be depressed while the key is hit.
Figure 1 shows the distribution of the APL characters over the keys.
Numerals, letters, comma, and period are in the customary positions;
however, there are only upper-case letters, which are typed without
the use of the shift key. The shift key must be used for almost all
special characters; the only exceptions occur at the end of each row
2 An Introduction to APL
<< >>2VA-t
1234567890 =
+ x
? u e P
rsj
t * i o * -
Q W E R T Y u J 6> p <-
a r L V A o t
D ( )
A S D F G // J a: L c ]
C D n u 1 T 1 *
.
\
Z X C V 5 // M » • /
FIGURE 1.
APL Keyboard
'"
Note that there are two forms of the minus sign: the sign (upper
character on the second key of the top row) is used as part of the
specification of a negative number, e.g., ~4 ; the sign - (upper
character on the second to last key of the top row) is used as an
arithmetic operator as in 5-2 . The symbol for division is * (upper
character on the last key of the top row). Note that the solidus /
(lower character on the last key of the bottom row) cannot be used
in the place of t . The zero (lower character on the third to last key
of the top row) and the letter cannot be used interchangeably.
1.2 SIGNING ON
(a) Make sure that the terminal switch marked LCL/COM at the
(b) Lift receiver of the phone next to the terminal and dial one
of the numbers listed in you get the
the local instructions. If
(c) Before trying to sign on, check whether the keyboard is un-
locked, i.e., whether depressing the shift key produces a
(d) If the keyboard is unlocked and you are signing on for the
first time, type a closing parenthesis followed without blanks
by your account number:
)9999
giving the number of your port (terminal), the time and date
of your sign-on, and your user code, which is distinct from
your account number. This is followed by a blank line and
by the headline A P L \ 3 6 . You are now ready to
start using the computer.
(e) If, at the time you are trying to sign on, somebody is in-
NUMBER IN USE
and will not be able to sign on until the other user has signed
off. To protect yourself against being locked out in this
4 An Introduction to APL
)9999:LOCK
vals. If, however, you forget your password (or your account
number), the response to your attempt to sign on will be
lows:
)OFF
and depress theRETURN key. When you next sign on, you
must then use the same password as for the current work
session. If, however, you wish to change the password, say to
NEWLOCK, sign off by typing
)OFF:NEWLOCK
have evaluated, you may discover typing errors. If you have not yet
entered the command or expression by depressing the RETURN key,
these errors can be corrected as follows. Backspace to the first erro-
neous character, and depress the ATTN* key. The response to this
will be the symbol v typed under the first erroneous letter, indi-
cating that this and everything to its right have been erased. Now
complete the typing of the command or expression and depress the
RETURN key.
the user, while the second, not indented, line is the response given by
the computer after the RETURN key has been depressed. In the
second example, note the raised minus sign used as part of the speci-
fication of a negative number. The customary minus sign is reserved
for use as an arithmetic operator.
13. 5 + 2187.
2500. 7
13. 5-2487. 2
"2473. 7
13. 5x200
2700
2700t13 .
200
12*2
144
2*0.5
1.414213562
5
The last example is concerned with the evaluation of 2° or \[2.
Note that up to ten digits of the result will be typed out unless you
specify another number, say 7, by typing the command )DIGITS 7,
and depress the RETURN key. The response to this command, indi-
cating the number of significant digits previously used, is shown be-
low together with a new evaluation of \f2. Note that the seventh and
following digits of the earlier, more accurate value were 3562. The
seventh (and last) digit of the new value is 4 as required by proper
rounding.
)DIGITS 1
WAS 10
2*0. 5
1.414214
The maximum number of digits that you may call for is sixteen.
Note that the DIGITS command does not limit the number of digits
you may use in input, nor does it affect the number of digits carried
in the computation; it simply specifies the number of significant
digits to which the result of a computation is typed out. It may,
Getting Acquainted With the System 7
however, affect the form in which this result is presented. The fol-
) DIGITS 5
WAS 10
100.2x200
20040
)DIGITS 4
WAS 5
100. 2x200
2. 004£4
) DIGITS 5
WAS 10
1. 1£4
11000
1. 1£5
1.1E5
0. 0008
0. 0008
0. 00008
8£~5
100056
10 5 6
8 An Introduction to APL
)DIGITS 3
WAS 5
1.1B2
110
1.1E3
1.1E3
0. 0008
0. 0008
0. 00008
8E~5
100056
100056
Q«-4-D«-1-Q«-3x[]«-8t2
12
"11
15
2 _ + 4 _
3 4 5 6
B«-3x4-2
U*2)+£*2
40
U*3)-£*3
208
The rule for the evaluation of a composite expression that was stated
earlier in this section must now be extended to include monadic as
well as dyadic operators. When an expression is ready for numerical
evaluation, the values of all quantities it contains must be known
regardless of whether these values appear in explicit numerical form
in the expression or are represented by the identifiers of variables. To
describe the manner in which a composite expression is evaluated,
consider, for instance, the expression of the symbolic form
(QoQooQ)ooQoQ
QooQoQ
QooQ
Qoo(QoQooQ)
13
14 An Introduction to APL
co
CT>
CD ^
CM I
^ c^
lo r*-
o to
CO
^ to
CO
tH
CO it
C"> O
CO to
tH
CO CM
LO LO
o I
tH CO
CO
CO
r- CO
CO CO
CO CO
LO CO
=t CO
tr> o
o
LO LO CM
CO o
CO =t
£^ LO
CM /<"""\ CO -• CM
r-i •i« xH
•l« LO CM /"> O
3- LO CM LO 5^ O
O CD Or)* •
CM O ^ O
O LO
•
O O w
vH +- o + i +
+ tH • >< £^ E-| &H LO
^«.
Uj
^ w rH CNJ
Uj O
-J -J
0.
^ LO I
^ o
X
Uj
•
T-i
3
Uj
Arrays (part 1) 15
the elements 1, 2, 4, 12, 365. Note that in APL notation the num-
bers are separated by blanks rather than commas. The next line de-
fines the desired factor in terms of N, and the last line shows the
resulting values that this factor assumes for the five values of N.
Note that in the third line the expression (X*N) * IN has been as-
merical values of the five terms of the series, these values will not be
automatically displayed but must be called for by the command T .
from the preceding example is still available, the third, fourth, and
fifth partial sums of the series for e° ° 5 can be obtained as shown
below.
t/3+T
1.05125
+ /4+T
1.051270833
+ /5tT
1. 051271094
-/3+T
0. 95125
0. 0512291667
-/5+T
0. 9512294271
e 1 =2(1[
3\
+ ^
5!
+ • • + n
+ 2n)\
+ )•
(1
t2x+/( 15) • • l+ 2x \5
2, 718281843
*1
2 , 718281828
the third row from the top and the fifth column from the left. If the
considered array is denoted by M , the element in question would
be denoted by Ml 3 ; 5 ] . Note that in APL the indices of an element
are separated by semicolons rather than commas and are enclosed in
Operator p
A vector V of, say, six elements will be said to have the size 6, and
a matrix M of, say, 3 rows and 8 columns will be said to have the
size specified by the vector 3 8 .t In APL, monadic use of the
operator p (rho), called the structuring operator, provides the size of
an array.
Thus, for the vector V and the matrix M above, p V and pM respec-
tively have the values 6 and 3 8 .Accordingly, pp7and ppMhave
the values 1 and 2, which give the dimensionalities of V and M.
*ln APL literature, the term rank is frequently used instead of dimensionality,
but this will not be used here to avoid confusion with the customary use of the
term rank in matrix algebra.
tin APL literature, the size of a vector is frequently called its length, but this
term will not be used here to avoid confusion with the customary meaning of
the length of a vector. Similarly, the term dimension, which is often used in-
stead of size, will not be used here to avoid confusion with the concept of
dimensionality introduced above.
18 An Introduction to APL
ments of the second row and those of the third row— say, the vector
2 3 5 We then structure this vector into the desired
7 11 13.
matrix M by the command O+M+2 3p2 3 5 7 11 13, where
the symbol pair HR preceding M calls for the typing of the matrix
identified by M Note that pM and ppM furnish size and dimen-
.
APL manuals, the term "ravel" is used for the operation expressed
by the monadically used comma; the term "unravel" might be more
appropriate.
U+M+2 3p2 3 5 7 11 13
2 3 5
7 11 13
pM
2 3
ppM
2
Q+V+JJ
2 3 5 7 11 13
2 2pK
2 3
5 7
.
Arrays (part 1) 19
PV
2 3 5 7 11
13 2 3 5 7
11 13 2 3 5
2 3 5 7
9qV
2 3 5 7 11 13
Operator i
15
12 3 4 5
75+. 25xi 5
.
V+\ 1
V+6 3 5 5
V\3
V\S
Vx2
A+l 5 6 2 3 6
VxA
15 2 1
M+2 3p/l
VxM
5 3 1
5 2 1
Indexing
Note that the command 7[12] in the example below produces the
message INDEX ERROR, beneath which the unacceptable command
is repeated with a caret suggesting what is wrong (in this case, that
the vector V has less than twelve elements). M[2;4] is the element
at the intersection of row 2 and column 4 of M . Similarly,
Ml 4 ; 2 5 ] is a matrix consisting of the elements at the inter-
sections of rows and 4 with columns 2 and 5. The examples op-
1
Q«-y«-l + 2xi9
7 9 11 13 15 17 19
D+M+^ 5p\V
3 5 7 9 11
13 15 17 19 3
5 7 9 11 13
15 17 19 3 5
7[6]
13
7[2 4 5 6]
5 9 11 13
7[12]
INDEX ERROR
Villi
A
ilf[2;4]
19
A/[l 4; 2 5]
5 11
17 5
M[;2 5]
5 11
15 3
7 13
17 5
Mil 4;]
3 5 7 9 11
15 17 19 3 5
called for in the sixth line are not made in the original vector V but
in the vector as changed by the preceding substitution. Similar
changes may be made in a matrix.
7«-l+i 9
M+3 4p7
7[4>0
3 40678910
V
7>1
7[3
3 10671910
V
M[l;2]«-5
M
2 5 4 5
6 7 8 9
10 2 3 4
Ml 2 3;1 4>20
M
2 5 4 5
20 7 8 20
20 2 3 20
fied R as the empty vector by R*-\ Note that the comma used as
.
PA
Monadic Operators
A+-~2 3 4
-A
2 "3 ~4
xA
"l 1 1
\A
2 3 4
A+2 2p\4
1 0.5
0.3333333333 0.25
24 An Introduction to APL
Dyadic Operators
A+1 3 5 7
£«-4 3 2 1
A-B
"3036
A*B
4 9 10 7
10x4
10 30 50 70
A+2 2pA
B+-2 2pB
AtB
0. 25 1
2.5 7
0.8 0.6
0.4 0.2
Special Operators
from right to left. It follows that +/A is the sum of the elements of
A , and x/A is their product. Note that -/A is the alternating sum
end of Sec. 2.1 ), and v /A is the quotient of the product of
(see the
A+\ 10
+ /A
55
-/A
"5
3628800
S10
3. 6288£6
r/A
0.24609
(1x3x5x7x9)^(2x4x6x8x10)
0. 24609
A<~3 3pi9
+M
6 15 24
+M
12 15 18
ments c^, c
2 , . . . , cn + 1
and n > 0. If the degenerate case n = is to
be included, the expression \pC in the command above must be
replaced by \ p , C because pC is empty if C is a scalar but has the
value 1 if C is a one-element vector.
A+l 3 5
B+~2 1 3
+ /A*B
16
A+ x£ .
16
Af-*- 2 3pi6
//«-3 2p"3+i6
/•/+.x//
4 10
4 19
(WO + .x$Af
4 4
10 19
true or false. Typical relational symbols are< < = > > * and e .
.
Arrays (part 1) 27
where; the second expression has the value 1 — < X < 2 and
for 1
[>J«-0. 5x"i|+i9
"1.5 1 "0. 5 0.5 1 1.5 2 2.5
011111110 l-(*<~l)+*>2
B+l 11
C+\ 5
B/C
14 5
S«-5p0
B/C
0+V+1/S+H
n+v+o/s
PV
X+2
Defined Functions
(parti)
arc sin X, arc cos X, and arc tan X, the notations ~ \oX , ~2o^
" 3oX are used in APL. For the hyperbolic functions sinh X, cosh X,
tanh X, and their inverse functions, see Sec. 5.1
29
30 An Introduction to APL
and global variables is the subject of Sec. 3.3, and some ways of
checking function definitions are presented in Sec. 3.4. Additional
material on defined functions is contained in Ch. 8.
Let us denote the growth factor in the second line of the first exam-
ple of Sec. 2.1 by G . The commands
£+-13 5 4.2
G«-(l + 0.05x/V)*W
D*G
will compute the final value of a deposit of $1354.20 at the end of a
year during which compounded monthly at 5%. If we
it has been
then want to compute the final value of some other deposit— for
example, $2485.00 at 5% compounded semiannually for a year, we
may enter the commands
£+-2485. 00
N+2
DxG
While D and N must obviously be redefined, it is irksome that the
command for G must be retyped although it has not changed. To
avoid this, we may define a function called GROWTH of the variable
N once and for all as described below.
cate that we wish to change from the execution mode to the defini-
tion mode, we begin the headline of a function by typing the charac-
ter V (called "del"). This is followed by the identifier of the value
of the function (here G ), a leftward arrow, the name of the function
(here GROWTH ), which must differ from the identifier of its value,
a blank, and the identifier of the independent variable (here N ).
VG+GROWTH N
the next line to indicate that this is to be the first line of the function
definition. We now continue this line by typing G«-(1+0.05t#)*/VV ,
where the final del indicates the desire to close the definition mode.
As long as the material from the current work session is we
available,
VGROWTHlOlV
V G+GROWTH N
[1] G+(1+Q.05*N)*N
V
1354. 2Q*GR0WTH 12
1423.48
2485. OOxGROWTH 2
2610.8
function GROWTH was not successful; the old function GROWTH had
to be erased before a new function with the same name could be
defined. If it is desirable to retain the old function, the new one must
be given a different name, for instance GR0WTH1.
VG+R GROWTH N
DEFN ERROR
VG+R GROWTH N
A
) ERASE' GROWTH
VG+R GROWTH N
[1] G+(1+R*N)*N V
3000x(0.065 GROWTH 4)*12
6503. 51
) ERASE GROWTH
VG+GROWTH V
[1] ff^(l+y[l]*7C2])*7[2] V
300Qx(GROWTH 0.065 4)*12
6503. 51
Note that if there is only one argument, this must be the right argu-
ment.
forget to depress the SHIFT key while typing the operator * be-
tween VI 1] and F[2] and hence obtain a x sign instead of the
t sign. If the error is detected before the command is entered, it
may be corrected in the manner described in Sec. 1.4. If, on the
other hand, we have entered the erroneous line, then we have left
the definition mode on account of the terminal "del" of this line. To
correct the line, we must re-enter the definition mode, correct the
line, and leave the definition mode. This can be done by typing
VGROWTHll]G+(l+Vlll*Vl2])*Vl2lV
)
3.2 BRANCHING
Growthtable
date the leading column of values of the interest rate (command [2] ).
The Jth row of this matrix is defined by command [6] which states ,
j- U) h01 (N d- h
o in om w ton
0000000
co co zt zt in in to
co lo to 00 cr> tH zt
O LO OLO O
tO *H
0000000
co co zt zt in in to
^ LO LO in
* vH CO CO 3- d- LO in to
•— N»^ O
S5:
tH vH rH rH tH *H xH
1
—
H
i—j
ft, 6s
X S3
vM Ss
O ^
• O LO LO LO
r— fei O ft; CO CO d- J- LO LO tO
^ <s O O O O
BQ
L_J
CtJ^ Ow ^-VTHrHrHrH
PQ &3 <-N —
l 1 .-->
«q; ^ o *h H x
£h L^ q. + t_J LO
ft: O ^ Sf^
^ CS Q. • HV n +
O + »^> •• LO
ft; ft. wo
ft,
a.
4-4'4'4'4'
t-h »^ h lo m in
^5 >*-'i 1 cn
co co zt zt in in to
tH cm co zt m to r^
Defined Functions (part 1) 35
After the Jth row of the matrix T has been computed according to
command [6] command [7] thus effects
, an unconditional switch
tocommand [5] As the parenthesis in the . latter command is evalu-
ated from right to left, the current value of I is increased by 1 and
the new value of I , which is the number of the next row to be
computed, compared to the fixed value of J If the J"th, i.e., last,
is .
row of the matrix T has just been computed, this new value of I is
J" +l , and since the assertion J<I in command [6] is true, the
parenthesis has the value 1. Since 1/0 has the value 0, the computa-
tion is terminated. If, on the other hand, the current value of I is
36 An Introduction to APL
Quadratic
V R+QUADRATIC C
[1] i?+-4p0
[2] ZM0.25xC[l]*2)-C[2]
[3] -KZK0)/7
[4] i?[l>-(0.5xC[l])-£*v2
[5] /?[3>-(0.5xC[1])+Z?*t2
[6] >0 f /?[2]^i?[4]^0
[7] /?[ 1 ]«-/?[ 3 ]«—0. 5xC[l]
[8] i?[4]+-/?[2>( |Z>)*v2
QUADRATIC 1 6
3
QUADRATIC "^ 13
3 2 "3
the commands [4] and [7] begins a series of commands that defines
the elements of 7? for nonnegative discriminant (real roots) or nega-
tive discriminant (conjugate complex roots).
The first two uses of QUADRATIC shown above concern the equa-
tions
x 1
= 2, x 2 = -3 and x 1
= 2 + 3/, x2 = 2 - 3/.
Zero
variable are given for which this function has values of opposite signs.
(A zero of a function is a value of the independent variable for which
the function has the value 0.)
V F+-T0L ZERO X
[1] -*(0<(FUNCTN Xlll)*(FUNCTN #[2]))/6
[2] -+(TOL>\G*-FUNCTN F+0 Sx + / X) / .
The method used to find the approximate value of the zero is called
binary search. It is an iterative method, in each step of which the
length of the interval known to contain a zero is halved. The itera-
tion is broken off when a value of the independent variable has been
found for which the absolute value of FUNCTN is equal to or less
the next step of the iteration. If, on the other hand, FUNCTN has
Defined Functions (part 1) 39
to show that no confusion arises from the fact that both the function
ZERO and the function FUNCTN which appears in ZERO use the
, ,
V F+-FUNCTN X
[1] F*-~2+X*X-3 V
1E~6 ZERO 4
3. 561553001
0. 5x( 3+17*t2)
3. 561552813
G
7 . 775970516£~7
X
VALUE ERROR
X
A
Since FUNCTN has the values "2 and 2 , respectively, for the argu-
ment values and 4, there is a zero in the interval (0, 4). The com-
mand 1E~G ZERO 4 yields 3.561553001 as the approximate
value F of this zero. A check can be made of this result by solving
the quadratic equation directly via the command 0.5*( 3+17*0. 5)
which yields the "exact" value 3.561552813. As stipulated, the
variableG, computed in command [2] of ZERO and equal to
FUNCTN F , has an absolute value less than the tolerance 1E~6.
V F+-T0L ZERO X
[1] (0<(FUNCTN Xll])*FUNCTN *[2])/4
[2] + (T0L>\G*-FUHCTN F+Q.5* + /X)/0
[3] -+2 Xll+(0>GxFUNC TN *[ 1
9 ] >F r
)
[4] 'ERROR'
V
1£~6 Z£i?0 3 4
3. 561553001
1£~8 Z£i?0 3 4
3. 561552815
For the purpose of defining a function, its arguments and value must
be given identifiers. In the function ZERO , for instance, these are
TOL , X , and F . There is no need, however, to employ the same
when the function is used. For example,
identifiers for these variables
after specifying A+-1E~ 6 and B+Q 4 we may call for the evalua-
,
X+0 4
IE" 6 ZERO X
Defined Functions (part 1) 41
the command X after the evaluation of the zero would have yielded
the initial value '+ , even though this value has been changed
during the execution of the function ZERO .
To avoid the inadvertent use of the same identifier for distinct vari-
ables,it is good practice to declare as local to a function all variables
whose numerical values are not likely to be required after the func-
tion has been evaluated or that can be readily recomputed should
they be so required. In ZERO , for instance, G could be declared as
local because its last value, if desired, can be obtained as shown be-
low.
U<-Z«-1E~6 ZERO i*
3. 561553001
FUNCTN Z
7. 775970516£""7
indicates that the variables i", «/, K are local to the function
GROWTHTABLE (see the beginning of Sec. 3.2), in which J and K
determine the size of the table and J is a counter used to terminate
the computation after J rows of the table have been obtained. The
42 An Introduction to APL
Note that the value of a global variable is not available during the
execution of a function that has a local variable with the same identi-
fier. For instance, command [4] could be omitted from the original
definition of GROWTHTABLE provided that the assignment J«- was
made before this function was invoked, but this change could not be
made in the definition in which I has been made local to the func-
tion.
AREA the variable H could not be made local to AREA Note that
, .
Defined Functions (part 1) 43
VCONVERGENCEtQlV
V P CONVERGENCE C\I
[1] I«-2*P[1]-1
[2] ->((2*P[2])<J«-2xJ)/0
[3] -*2,D«-J.(I 4P£V1 C)
VJ[D]V
[1] F+X*2
5 8 CONVERGENCE 1 3
32 8.66797
64 8.66699
128 8.66675
256 8.66669
VC0NVERGENCEIQ1V
V P CONVERGENCE C\I
[1] J>2*P[1]-1
[2] ->(2*P[2]<J^-2xJ)/0
[3] +2,n^#,U AREA C)
VAREAIU1V
1 2 CONVERGENCE 1 3
TLC0NVERGENCE+1 2
1 2 CONVERGENCE 1 3
CONVERGENCES'] 1
C0NVERGENCEL2]
TACONVERGENCE+0
It is seen that command [1] yields the correct result 1 (initial value
of I ), while command [2] furnishes the incorrect result (instead
of the empty vector, which indicates branching to the next com-
mand). Indeed, evaluating command [2] from right to left, we first
1 2 CONVERGENCE 1 3
SYNTAX ERROR
AREAL31 A+H*(+/Y C)+0.5(Y 5[1])+Y 5[2]
A
)SI
AREALZl *
C0NVERGENCEL31
+
VAREA [3D1]
[3] A+Hx(+/Y C)+0.5(Y fl[l])+7 B[2]
1
[3] A+H*(+/Y C)+0.5*(Y S[l])+7 5[2]
[4] V
)SI , which stands for status indicator. The response to this lists all
the number (here [4] of ) the next command (if there was such a
*The digits from 1 to 9 may be used in this manner to demand the insertion of
up to nine empty spaces, and the letters A B C . . . may be used to
f f
indicate the need for 5, 10, 15, . . . empty spaces.
I , ]
1 2 CONVERGENCE 1 3
VALUE ERROR
CONVERGENCE^^! +2 ,Q<-H (I 4i?E4 C)
A
)SI
CONVERGENCES! *
—r
V ARE Al QUI]
CO] A+N AREA B\H\C
J
[0] A+N AREA B;C
Cl] V
1 2 CONVERGENCE 1 3
0.5 8.75
0.25 8.6875
0.125 8.67187
0.0625 8.66
)SI
AREAL31 *
C0NVERGENCEL31
VCONVERGENCEL 2Dl
[2] ((2*P[2])<I«-2xI)/0
1
[2] -*((2*P[2])<J>2xJT)/0
[3] V
1 2 CONVERGENCE 1 3
1 9
0. 5 8.75
interrupt this, we depress the ATTN key. The response to this indi-
cates the command whose execution is to be completed next. We
again request a status report and clear the status indicator. The fact
that the computation is not terminated shows that the desired
branching to [0] in CONVERGENCE^] is not performed. Inspection
of this command reveals that the rightward arrow has been omitted.
After this mistake has been corrected, 1 2 CONVERGENCE 1 3
finally yields the correct result.
System Commands
(parti)
The ease with which a user may build his own function library or
copy functions from a public library or another user's library is a
major asset of the APL system. The two brief sections of this chapter
show how a user may start his function library and make additions
to it. A more complete discussion of system commands is found in
Ch.9.
49
50 An Introduction to APL
Because the systems command )C0NTINUE has been used at the end
of the last work session, the content of the CONTINUE workspace is
copied into the active workspace when we sign on. This is indicated
by word SAVED and the time and date of the
a line containing the
last sign-off. Note that the active workspace, which is a copy of the
A P L \ 3 6
GHZ
AREA
7.
)VARS
G
CONVERGENCE
775970516£~7
ZERO
) ERASE GHZ
) GROUP ARCON AREA CONVERGENCE
)WSID LI BR
WAS CONTINUE
)SAVE
11. 06.19 07/01/70 LIBR
) CLEAR
CLEAR WS
)COPY LIBR ARCON
SAVED 11.06.19 07/01/70
)FNS
AREA CONVERGENCE
)GRPS
ARCON
)VARS
) CLEAR
CLEAR WS
)COPY LIBR
SAVED 11.06.19 07/01/70
)FNS
AREA CONVERGENCE ZERO
) VARS
)GRPS
ARCON
If we need only the functions of the group ARCON we may, use the
system command consisting of the characters )COPY followed by
the identifier of the workspace (LIBR) containing the object (vari-
time. Ifwe had not formed the group ARCON we would have had
,
The response to our copy command indicates when LIBR was saved,
and the responses to requests for listings of functions, groups, and
variables in the active workspace show that we have the functions
AREA and CONVERGENCE and the group ARCON , but no global
more clearly
variables with numerical values. (To exhibit this absence
in the example, the paper has been manually advanced by one line
member of the library LIBR , that the command )OFF was used
to terminate this session, that the functions AREA and CONVERGENCE
System Commands (part 1) 53
A P L \ 3 6
)C0PY LI BR
SA VED 16.40.47 07/01/70
)WSID LI BR
WAS CONTINUE
)SAVE
16.44.56 07/01/70 LIBR
) CLEAR
CLEAR WS
)C0PY LIBR
SAVED 16.44.56 07/01/70
)FNS
AREA CONVERGENCE ZERO
)VARS
)GRPS
ARCON
In Sec. 1.5, only the most frequently used operators were treated,
and for some of them only their monadic use was discussed. Section
5.1 is concerned with the dyadic use of the latter operators, while
Sees. 5.2 and 5.3 introduce new operators in both monadic and
dyadic use. Basic operators are defined in Sec. 5.4.
The monadic use of these operators was explained at the end of Sec.
1.5, and some of the dyadic uses of o were mentioned at the be-
55
56 An Introduction to APL
en
oo
en
CO
•
00
to J"
O CM
CM zt
O ID
CO O
• •
o o
cm en
-H O
r- oo
r- o
zf en
• •
o o
CO
co O
oo en r-
o o • Co
h m co > fei
o zt to fe:
oo co =t Cj H
• • >< • Co
o o uo o.
o — zf • Ctj x cm
tH :* oo o 55; ><
r* CM +- H O o
® CO H ^ CO H
O vH CO | h X LO
tH
CI
in
rH
cm
J"
.^tn
CM jj-
OO
+ \ o
IT) CO tH vH vH CO 4" I
The meaning of MoA and ( -M)oA for 0<M<7 is given in Table 5.1.
TABLE 5.1
M MOA (-M)oA
(1-A*2)*0. 5 ( 1->1*2)*0. 5
1 sin A a resin A
2 cos i4 arccos A
3 tan 4 arctan A
4 (l+4*2)*0.5 (~1+A*2)*0.S
5 sinh A arsinh A
6 cosh i4 arcosh A
7 tanh A artanh A
Since these operators have not yet been discussed, both their mon-
adic and dyadic uses will be explained below, where M and N de-
note positive integers.
On the real number axis that is directed toward the right, the points
with the abscissas I A and [A are the points with integer abscissas
that are immediately to the left and right of the point with the
58 An Introduction to APL
""4
abscissa A . For example, L~3.2 and T 3.2 have the values
and "3 , respectively. If A is an integer, both IA and \A have the
value A .
The function SMALL shown next uses the operations just discussed
to locate the indices I and J , and the value At I ;J] , of the
smallest element of a matrix A (or one of several mutually equal
elements that are smaller than any other element).
V S+SMALL A;V;I;J
[1] I+V\L/V+l/A
[2] J+V\L/V+L/A
[3] S+I t J,AlI;Jl
V
each element having the same chance of being chosen. For a given
?10p6
1 5 3 4 2 1 5 5 6 3
?10p6
4 5 114
CLEAR
)
5 1 3 1 3
CLEAR WS
?10p6
1 5 3 4 2 1 5 5 6 3
?10p6
4 5 114 5 1 3 1 3
) WIDTH 5 5
WAS 120
11 10
(?36p6)+?36p6
7 12 8569886829
5
5
6
11
11
87
10
10
6
888536
8 7 10 10 9 2
provision that any element may be chosen only once. Thus, N?N
is random arrangement of the elements of N and 2 ( 2*N)?2*N
a t , I
5?9
5 4 2 8 1
864 1 ?1
10 597213
1010101100 2
235813213455
V+l
| 1 ?1
Vl(f>V)?pVl
3 21 34 8 13 55 5 1 2
The expression BaC, where B and C are Boolean scalars, has the
value unless both 5 =1 and C = l , in which case BaC has the value
1. This meaning of the operator a is displayed below as the response
to the command 0011A0101 , and similar displays are
given for the operators v , * , and v .
Additional Operators 61
B+ 1 1
V+l 2 3 4
B/V
3 4
( ~B ) V
1 2
O-O 1 1
5aC
1
SvC
1 1 1
B*C
1 1 1
B»C
1
v5iM/yc#[;n;]v
V B+BRANCH V
[1] 5^-8x((7[1]>5)a(7[2]<3)v7[3]=6)
V
BRANCH 7 2 4
8
BRANCH 7 2 6
8
BRANCH 5 2 'I
V+\b
Q+W+VL5 7 51
4 5 3 12
0+R+V>W
Oil
n+s+-v>w
ill
RaS
Oil
/?v5
111
Arrays (part 2)
cards, in which each card carries a matrix of the same size. For
example, an array A of the size 3 2 M- corresponds to a deck of
three cards, each of which carries a matrix of 2 rows and 4 columns.
The element A[ I;J;K] is found on the Jth card at the intersection
of the JXh row and Kth column. The display of such an array is card
by card, as shown in the example below, which also indicates how
the elements of a vector V are structured into an array of dimen-
sionality 3.
Note that the command ,A yields the vector V from which A was
formed. Note also that the commands i4[3;2;4] and i4[3;l 2;4] ,
63
64 An Introduction to APL
[~K4«-3 2 4p7«-t24
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 19 19 20
21 22 23 24
1 2 3 4 5 6 7 8 9 10 11 12 13
14 15 16 17 18 19 20 21 22 *
23
24
i4[3;2;4]
24
A[3;l 2 ;4]
20 24
4[2 3;1 2;3]
11 15
19 23
A[_2 3; ;: 5]
11 15
19 23
A{_7 3;1 2;2 3 4]
10 11 12
14 15 16
IB 19 20
22 23 24
of size 2 2 and an array of size 2 2 3. Finally, note that the two
central indices in A\_2 3;1 2;3] exhaust the range of the second
index of A and may therefore be omitted.
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
All ;2;1;3]
All ; ; ;3]
3 6
9 12
At; ;;3]
3 6
9 12
15 18
21 24
1 "l 1
1 ~1 1
B-~B
1 "1 1
1 1 "1
1+ 2x5
111
1 "l "1
The rules for using basic operators with vectors and matrices that
were given at the beginning of Sec. 2.3 and further illustrated in Sec.
0+-A + 2 2 3pil2
12
4 5
3
6
7 8 9
10 11 12
Arrays (part 2) 67
\A
1 0.5 0. 33333
0.25 0.2 0. 16667
3 6 9
12 15 18
21 2 4 2 7
30 33 36
D+B+2 2 3pl3-ll2
12 11 10
9 8 7
6 5 4
3 2 1
A*B
12 22 30
36 40 42
42 4C 36
30 22 12
Reduction
*/10 2 3
1EB
®/10 10 100
0. 30103
a/1 1
v/1 1
D«-i4*-2 3 4pl 1 1 1
1111
111
10 11
110 1
1110
1111
2 122
12 2 1
2 12 2
= /C2]i4
11
110
Arrays (part 2) 69
I/, A
r / ,a
Inner Product
The scalar product and the matrix product discussed in Sec. 2.3 are
special forms of an operation that is called inner product. It involves
two arrays A and B that must be conformable in the sense that the
last element of the size of A must be the same as the first element of
the size of B . The result of the operation is an array C , whose size
is obtained by deleting the last element from the size of A and the
first element from the size of B and catenating the results. For
example, if A and B have the sizes 2 3 4 and 4 5 6, respec-
tively, then C be of the size 2 3 5 6. For conformable arrays
will
reduced by d/.
The following examples show uses of the inner product. The function
POL evaluates the polynomial c, + c
2
x + c3 x 2 + • • +c
n+
x n the 1
;
power series. The function DECIMAL yields the decimal form of the
number whose binary digits constitute the vector B The function .
VPCL[D]V
V P+-C POL X
[1] P«-C+. xJ*~1-m p ,C
V
12 3 POL 5
86
VERRORIW]^
V
5 ERROR .
1. 0576£~6
VDECIMALIU1V
V D+-DECIMAL B
[1] £«-£+. x2*(p ,B)-ip ,5
V
DECIMAL 1
1
DECIMAL 10 11
11
VCOMBIU1V
V C+M COMB N
[1] C«-(iM) + IMpN .
V
3 COMB 5
25
(2-tf*2)r.Ll + (#«-0.1x(""H-ill))*3
1. 36
Arrays (part 2) 71
zf zf J- J" ^
CD CM CO zf O
O CO C^ zt CO
rH CM
to r^ cd cm zf c*
o in om h idh
zj-
co co d- 4- in in <d
o o o o o o o
ID CD ID ID ID
CO IT) t^ C75 H
•••••••
HrlHH HdH
O CM (D CM tH
xH CM
CO LO (D CO Ol H 4
o in o in o id T-t
co co zf zf in in co
o o o o o o o
CD ID CD CD CD
vH CD r^ in CO rH vH tH -rH tH rH <rH
O T-i in CD
tH
=5
Q.
zf
CM CO zf LO co 00 CD
O LO o LO o LO O
* CO CO zf zf LO LO CD
O o o O o o O
LO
r-»
zr
o
zi-
zj-
zr zf zf
00 CM CD P-
^
•!• tH tH tH vH tH tH tH
+ «-• •
tH o rH zf O X CM O
CM tH *H in PC
* * • X
*i •-"* o zl- rH
X X + O
CM + in CM •
• • • O LO LO LO
o o CM tH + CO CO zf zf LO LO CD
* >1 + * tH O o o O O o o
X sm^ PC ^ W
O tH zf CD CO
72 An Introduction to APL
Outer Product
example on the last page shows how the outer product may be used
to produce the body of a table of squares, in which the line of an
entry corresponds to the integer part (0, 1, 2, 3, 4) and the column
to the fractional part (0.0, 0.2, 0.4, 0.6, 0.8) of the argument. For
example, the element in the second row and third column of the
table is the square of 1 .4.
The following examples show how the outer product lends itself to
the construction of useful matrices. For example, the matrix S in
U+S+-Vo.=V+\3
1
1
1
v° ><v
1 1
1
Arrays (part 2) 73
Vo.>V
10
110
111
7o.+3p0
111
2 2 2
3 3 3
VPOLYNlUlV
V P+C POL IN X
[1] ( Jo *~i +ip ,C)+. *C
V
12 3 POLYN 2 "1 1 3 5
9 2 6 34 86
column of this matrix serve as row labels and give the integer part
of the argument. The elements in the first row are column labels
and give the fractional part of the argument. Outer products are
used in commands [3] (outer product of two vectors) and [6]
(outer product of vector and matrix, which is itself the outer product
of two vectors).
74 An Introduction to APL
O
00 ID CN 00 J" 00 CD CN 00 3" O
oocor-zfco o co co or- co
H CN CO Zf ID r** Cn
ID ID tD (D <D CD ID (D ID ID
ID CO LO CT tH h CD CO tf"> C^ <T> tH
OO (NID(\H
H CN
O H CO
CO It
h
IT)
CO (N
C- CD
CD CD CD CO CO ID (D ID ID ID
.3- o m
•«-* r^- co j-THO^t^mco
oOxhiOtHCD ocnoj-ooo
tH tH CN t LOh CO
CM
*
/—\
•-\
>< ^-^-zl-Ji-J- 3-Z1-3-J-Z1-
O O t
LO>-iO
4-
• ••••••
CN
O O O
J" 00 CN ID
tH -=t" t"«- O
••••••
CN
I*-CO
CO CN CO
t-I C^ -3-
— X >i H H CN CO IT) ID 00
+ CN >-/
iH . -f-
I O •
4 4 o
N >< tsi
•• • o 4
>i O Q. I—
•* O t<3 '*"* LO
>< /-s q. 4 to *~
> •• Co '—n • — I + O O H J" Ol (D O LD ID Ol rlJ
t— i Co — CD rl OH
.. w ..
tH CN CO tCD CO
+.
i i
ki h to ld { lo cn
Cq ^1 - 1—1 ^
k^ CQ ^ • 4 •• 4 k]
*q; Si in >—' •• •» •» dq
EH 4 4 4" «—ii ii — i ^
> EH «X) E^ E-s EH EH EH
H CN CO j- LO
Arrays (part 2) 75
the same size as M All elements of the Ith row of N have the value
.
IxC for l<I<(pAO[l] This means that no element will change its .
row when 9 M+N io ordered and then structured into a matrix of the
size pM .
1
1 If I is positive,
. W consists of the first I elements of V or, if
I>pV of the elements of V followed by I-pV zeroes. If I is
,
of ( 1
1 ) -p V zeroes followed by the elements of V . If J= W is ,
empty.
identical with V if 1= .
76 An Introduction to APL
o
Q-
r-i
CM
l—l
Q.
+
O
Q.
>
CM I —
I I ft; ^• cm ^
ft; Cq I I I
ft; Q. C^ CM CO O
CO O =t ft;
I
J" ^ vH O Q- o
CO -^ o ft; w co ^
I
\ ^ U CM N
^
I I ft:
>
+ \ CO C-* tH
ID CM ft- ft; ^ ft:
I
*H CO zf CO CM
CO
CM |
Arrays (part 2) 77
accordance with the rules given above for a vector as a right argu-
ment. For example, for a matrix M of the size 3 4 , the matrix
2 "~3+Af consists of the elements at the intersections of the first two
rows and the last three columns of M . Similarly, for an array A of
the size 4, the array that consists of only the first three ele-
3 3
ments of the first rows of cards 1 and 2 is obtained by the command
2 1 3U .
The examples below illustrate these rules. Note that the command
~l + 7 which takes only the last element of the vector 7 furnishes
, ,
Note also that the command ~3 ~4+A/ adds a first row and a first
(2+7) ,"3+7
12 4 5 6
2+~l+7
3 4 5
6
qW
1
1234560056 ( 8 t V ) , 4+ V
M+2 3pi6
pQ«-"l ~1+M
6
1 1
"3 ~k+M
12 3
4 5 6
1+M
2 3
5 6
size 3 7 , the first five and the last two columns of C consisting of
the matrices A and B , respectively.
(Note that the value of ALU does not affect the result and may
therefore be chosen arbitrarily.) For example, 1 24 60 6 01
10 13 9 3 is the number of seconds in 10 days, 13 hours, 9
minutes, 30 seconds. Accordingly, 222H011 is the decimal
representation of the binary number with digits 1, 0, 1, 1. Note
that the same result is furnished by the command 2ll 1 1 .
1 24 60 60 1 10 13 9 30
911370
2 110 11
11
3.1 l 2.2 "1.5 ""2.6 7
50. 065
0.3 l *!5-i5
1. 3498
follows from the first example given for the operator i that
24 60 60T911370 has the value 10 13 9 30 while ,
examples are given below. Note that for a vector A and a scalar S ,
24 60 60 T 911370
1 13 9 30
60 60 T 911370
9 30
60 60 T 911370
253 9 30
(5p2)T28
1110 (7p2)T28
1110 (3p2)T28
10
(0,(3p2))T28
3 10
80 An Introduction to APL
0. 31t !4>0, i 4
1. 3498
A+2 2 3pil2
4>C2W
12
4 5 6
3
10 11 12
7 8 9
Note that the first example above gives another command yielding
3
the fifth partial sum of the power series for e° .
2<t>\ 5
3 4 5 12
~2<t>i5
4 5 12 3
A+3 2 2pil2
2<t>[l],4
9 10
11 12
1 2
3 4
5 6
7 8
Transpose
A+2 2 3pil2
1 4
2 5
3 6
7 10
8 11
9 12
3 1 2W
1 7
2 8
3 9
4 10
5 11
6 12
1 2 1M
1 4
8 11
umns, cards, etc.) from an array, expansion inserts groups that con-
sist exclusively of elements of value 0. If B is a Boolean vector of
more than (p4)[ J] elements such that + /Z? equals (pA)U'] then ,
while all elements not copied from A have the value 0. For example,
if A+3 3p\9 and B+l Oil then B\[_1]A is of size 4 , 3 , with
the second row consisting exclusively of zeros while rows 1, 3, and
4 are respectively identical with rows 1, 2, and 3 of A . Similarly,
4«-3 3pi9
1 1 0/LHA
12 3
4 5 6
1 1/A
1 3
4 6
7 9
84 An Introduction to APL
10 1 1\[1]4
1 2 3
4 5 6
7 8 9
1 1 1\A
10 2 3
6
4 5
7 8 9
Character Manipulation
85
86 An Introduction to APL
line. Note the alternative ways of handling short lines, i.e., lines of
P«-6 13p f !
Pllil+'THIS IS A DI- X
P[5;> !
TJ<9// OF A PA-'
L+' GE.'
P[G;ipL>L
THIS IS A DI-
MINUTIVE PAGE
ILLUSTRATING
THE ORGANIZA-
TION OF A PA-
GE.
P[4;]
THE ORGANIZA-
contain the text followed by a line with only the closing quote.
Short lines, such as the last line of the example, must be extended to
the standard length by the addition of spaces. Since the carriage
return at the end of each line is counted as a character, each line con-
tains fourteen characters. The carriage return after the opening quote
is an additional character, so that the response to the command
pPi4 6# would be 99. In organizing the text as a matrix of size 7 14 ,
PA GE+- '
THIS IS A DI-
MINUTIVE PAGE
TO ILLUSTRATE
THE ORGANIZA-
TION OF A PA-
GE IN FORM OF
A MATRIX.
M+-1 l^pliPAGE
ML 3;]
TO ILLUSTRATE
ML 3 ; 1 5 ]
TO IL
Note that the command M now would furnish the text typed double-
spaced because the carriage returns included in the text are added to
the normal carriage returns used in the output of a matrix.
Z in the second line has only been typed to show that four spaces
have been entered after the word LINES. Since this Z is the thirty-
ninth character of the input, and only thirty-eight characters are
needed for the matrix C , the Z does not appear in the output.
on
AN EXAMPLE OF CATE-
NATION OF LINES Z
C*-2 19pC
C
AN EXAMPLE OF CATE-
NATION OF LINES
CC2;]
NATION OF LINES
be done by enclosing the text segments in quotes and separating
them by semicolons from the commands for output, as shown in the
example below.
M+2 2p5-i4
'THE TRANSPOSE OF THE MATRIX' ;M;'
IS THE MATRIX' ;§M
THE TRANSPOSE OF THE MATRIX
4 3
2 1
IS THE MATRIX
4 2
3 1
Note the quote at the end of the first line, which indicates that the
character input is not yet complete. If this quote were omitted, an
error reportwould be received upon entering the first line on account
of the terminal semicolon. The symbol (typed by overstriking n
ft
ft EVALUATION OF POLYNOMIAL P
ft C=VECTOR OF COEFFICIENTS
ft BY DESCENDING POWERS OF X
C+-2 3 "4 5
X+2
U+P+XLC
25
Character Manipulation 89
The only basic operators that have meaning when used with character
data are = and * For example the expression PEARS
.
' =
'CAT' *. = '
HAT'
Af+2 5p f
APPLESAUCE'
M
APPLE
SAUCE
90 An Introduction to APL
p 9 M
10
2 5
*
ABCDEFGHIJKLMNOPQRSTUVWXYZ* \M
1 16 16 12 5
19 1 21 3 5
Me' A'
10
10
<M
ELPPA
ECUAS
SAUCE
APPLE
§M
AS
PA
PU
LC
EE
2 1 ±M
1 "1 \M
SAUC
10101010 1\M
APPLE
SAUCE
Character Manipulation 91
The function WORDS on page 92, which extracts the words from
a phrase, uses some of these operations. Its right argument PHRASE
is the character vector corresponding to the phrase. Command [2]
Note that the last two commands of WORDS could not be combined
Note that the function WORDS will not yield the desired result if
there is a space between the last character of the input phrase and
the final quote. This possibility is taken care of in the function
WORDMATRIX shown on page 93, which organizes the words of a
phrase as a matrix and thus makes them available for further manipu-
lation.
92 An Introduction to APL
to
ft;
ft:
>i
X
Co Co
ft: R
o? ft:
ft. o
o ^
^ o
ft- <o
ft; ft-
Ok! fej
cc; Co ft:
^ ^ ft: -
E-s
- ft: ft,
^ VU ftn - O
H•• Co H cq Co Eh
^ I to s; co
ts ft: ft: s$ q- ftqto
co ft: co ft: ft: £s co
> ^ px, ^c ft: ft, ft: ^:
wC^ftn-)- CQ ^ &:
r-i
i_j
Ctq
s^
fx,
v^ { hh^;
4.ft,H4-ift:
-ft:
4- f^
Co Cqa v I^H ^ fc}
R Co CoVvhCo + C^ CqCo
ft: Ci ^ h «-
^ <h - ^ g>
o ft^oii: x »h x a; ft: ft:
^ O4'ft:J-t^ft:J-4'C0 ft: o ^
> ^hfin + tft)+D+ ft>.^: O
H
> > Sh to
Character Manipulation 93
Gq -
CO
^
ft: X
ft: H
ft, ft:
\ Eh
/""N ^:
» ^
3 Ci
tS3 ft:
>-i o
X ^
^
^ ^
^ o
Eh H
CO Eh
ft: O
Q? ^
ft, ^
o ft,
CJ ^
ft: ^ ft)
o 1-5 ft:
^m
•
^ » Eh
^>
>< H i ft,
• « ft: t-» m o
^ CJJ x-
-
3
**k ft, fcj Eh
H•• ft] CO w
•-^
CO
Gj ^c ft) ft)
fej o ft: ft) Eh co
CQ OQ ft: CO ^c
^C ^C ft. ^ ft:
ft: -> ft: ^ J5:
ft: —
w
H ft:
o>
ft,
ft. + ft,
> ft) ft: ft: co ><
—
1
>< co co co
a H ^c ^ ^C 4- ^ / ft:
i i ft: ft: ft: ft: (^ 4- ^• CO Eh
*< Eh ft: - ft: ft: Oq r-i H ^c
H ^ ft. ft, ft, O Q> /*"S ft: ^
ft: ^ v-/ •• >^x -> ^ vH Eh c>
Eh Q O W Q \ vH w o
ft:
+• -
s
^
ft:
o 4-
4'
ft)
/ "* N
ss:
.
HV +
ft)
1\^
<rH
H +
^
a. +
4<
ft]
O
^
ft:
which the first word is entered by command [8] Command [9] adds .
an empty row to this matrix, into which the second word is entered
when command [8] is executed again.
Note that sorting on the first three characters does not produce the
desired order, but sorting on the first seven characters does produce
it.
*
O
CO
Q. 63
Hi:
6^
o
CO C3
o OQ
o
H
o
63
CO 65
63
C3
Eh
Q.
o O -
Co -o- ft;
I I
O
O 3-
CO
CO co
&3
^ Co .
r- CN
Ouu^
^ «*C
^ Co • • -Co 6j
6q 6q 6a 63 fel 63
DQ ooo O O no
C3 G>
95
96 An Introduction to APL
not yet implemented at the time of this writing. The same extension
will be provided for the operator T .
VCODELQlV
[4] +5*((pCLEAR)>S)
[ 5 ] C+-C BASElALPH ( 5 \CLEAR )
, i
[2] C+\0
[3] V+5pBASE
[4] 1+0
[ 5 ] ->6 x ( p CIPHER ) >I«-I + 1
[6] C+C .ALPHIVTCIPHERII'}']
C7] ->5
V
CLEAR+' RETURN IMMEDIATELY'
U+CIPHER+31 CODE CLEAR
16792222 13742716 4745 4 69 4999986
31 DECODE CIPHER
RETURN IMMEDIATELY
Character Manipulation 97
The remaining commands take care of all special cases that may
occur. For example, commands [16] to [18] furnish the output
NULL if the sum of the three elements of X is zero, or switch to
command [19] if this condition is not fulfilled. Command [19]
switches to [24] or [20] depending on whether the first element of
X is or is not 0. Command [20] switches to [21] or [23] depending
on whether the first element of X is or is not 1 . If XL 1 1 has another
positive value, say 3, command [23] will yield the character vector
DREI HUNDERT , because this will be the beginning of the desired
German numeral. (Note that that command [38] will, at the end,
delete all The reader should not experience
spaces from the numeral.)
any difficulty in following the remaining commands of TRANSLATE.
However, he should note the comparatively large number of special
cases that must be considered even for this extremely simple trans-
lation problem.
98 An Introduction to APL
VTRANSLATElDlV
V Z+TR AN SLATE X;A;B;C
[I] A+'NULL EINS ZWEI DREI '
[4] A+ 10 7 p>!
[5] B+'ZEHN ELF Z WO ELF f
[24] ->(0*Z[2]+X[3])/26
[25] -+38
[26] -+(0**[2])/29
[27] Z+Z .AlXZ3l+lil
[28] +38
[29] +(l**[2])/32
[30] Z«-Z,fl[X[3] + l;]
[31] -^38
[32] +(0=X[3])/37
[33] +(l**[3])/35
[34] Al2\~\«-*EIN '
[37] Z+Z>ClXt2l-l;l
[38] ZHZ* % X
)/Z
V
TRANSLATE 10
ZEHN
TRANSLATE 2 00
ZWEIHUNDERT
TRANSLATE 308
DREIHUNDERTACHT
TRANSLATE 560
FUENFHUNDERTSECHZIG
TRANSLATE 99 9
NEUNHUNDERTNEUNUNDNEUNZIG
8 Defined Functions
(part 2)
The great variety of features that APL provides for the definition of
functions is an important asset, but it is apt to confuse the beginner.
For this reason, the discussion in Ch. 3 was deliberately restricted to
a few essential features. The present chapter completes this limited
information. It contains sections on headline types, branching, the
use of labels, the checking and editing of function definitions, error
reports, and recursive functions.
All functions discussed in Ch. 3 had right arguments, and some also
had left arguments. Moreover, all these functions had explicit results—
that is, the function headline started with V followed by a dummy
identifier for the output, followed by a leftward arrow. There are,
however, other types of function headlines. For example, in the
headline of the function WORDS of Sec. 7.2, there is no identifier for
the output, because the output is caused by the symbol pair ["]-<- in
101
102 An Introduction to APL
replace each by a circle (o) and each 1 by an asterisk (*). The func-
tion PICTURE below accomplishes this; it has neither arguments nor
explicit result.
VPICTUREIQ1V
V PICTURE
[1] n+'o*'ZB00LEAN+l]
V
0+B00LEAN+5 5p0 1 1
Oil 1
10 1 1
110 1 1
Oil 1
10 1 1
PICTURE
o**o*
*o**o
**o**
o**o*
*o**o
8.2 BRANCHING
There are, however, many other types of switch, and three important
groups of these are discussed below, where the symbols ,a ,... and
1 2
/7
1
,a?
2
,... respectively denote assertions and command numbers.
.
->a p/?
l 1
and /7-xpia-.
Note, however, that the last type of switch will only operate properly
with index origin 1 (see Sec. 9.1), because with index origin 0, the
expression il has the value 0, while tO is empty. The switches of
the second group effect branching to commands n y
or n
2
(neither
one of which needs to immediately follow the switch) according to
whether the assertion a 1
is true or false. The types
Note also that this switch requires index origin 1. For index origin 0,
-*C*n n2 nm
y ,
8.3 LABELS
[3] and [4] the new command takes the number [4] and the old
,
lowed by a colon between the number of the command and its first
character. No matter how the commands of a tentative function
definition are reshuffled to obtain the final form of this definition,
Note that a label is local to the function in which it is used, but its
function.
ward arrow, and the vector (stop control vector) consisting of the
numbers of the commands just before whose execution the computa-
tion is to be temporarily halted. For example, to obtain the desired
G
values of F and X in command [2] of ZERO, we give the
, , ,
VTRANSLATE101V
V Z+TRANSLATE X;A;B;C
[I] A+'NULL EINS ZWEI DREI "
[4] A+ 10 7 pA
[5] B*-'ZEHN ELF ZWOELF
[6] B+B 'DREIZEHN
t VIERZEHN FUENFZEHN '
[9] £«- 10 10 pS
[10] C+'ZWANZIG DREISSIG VIERZIG f
[13] C^ 9 9 pC
[14] Z«-i0
[15] X+-(3plO)T*
[16] -*(0*+/*)/Ll
[17] Z«-Z t i4[l;]
[18] -*0
[19] £1:-K0=X[1])/L3
[20] +(1*Z[1])/L2
[21] Z«-Z t <7[9;]
[22] +L3
[23] L2 :Z*Z t ilCX[l 3 + 1 ;].(7C 9;]
[24] L3:+(0*X[2]+;:[3])/L4
[25] ->L9
[26] L4:+(0**[2])/L5
[27] Z«-Z,4[*[3] + l;]
[28] +L g
[29] L5:+(l*X[2])/L6
[30] Z«-Z,£[X[3] + 1;]
[31] ->£9
[32] L6:-*(0=X[3])/L8
[33] -+(1*X[3])/L7
[34] A12;1+'EIN "
[36] ->L 9
Defined Functions (part 2) 107
TRANSLATE
NULL
TRANSLATE 16
SECHZEHN
TRANSLATE 2 31
ZWEIHUNDERTEINUNDDREISSIG
TRANSLATE 460
VIERHUNDERTSECHZIG
TRANSLATE 70
SIEBENHUNDERT
108 An Introduction to APL
binary search for a zero of the function FUNCTN in Sec. 3.2 has been
initiated by a command such as 1E~6 ZERO ^ , the computation
is halted after the first execution of command [2] and ZER0[3] is
,
gives the second set of values of these variables, and the two sets may
be sufficient to indicate that command [2] operates correctly or that
it has been incorrectly formulated. The command -* will then ter-
Note that erasing a function on which a stop control has been set also
erases the stop control vector. Editing of a line for which a stop con-
trol has been set removes the stop control for this line. Similar state-
ments also apply to a trace.
Insertion of a Command
Suppose that the need for insertion of the command -*2x(ptO<T be-
tween commands [3] and [4] is discovered when the computer has
asked for command [7] . To achieve this insertion, complete the line
as shown below:
When the line is entered, the command is inserted with the number
[3.5] and the system asks for command [3.6] . If no further com-
. ,
Note that any number between 3 and 4 could have been used instead
of 3.5. If, for instance, 3.28 had been used, the system would have
asked next for command [3.29] , and this number could have been
overridden by [7] in the same way as above.
If the need for the considered insertion is discovered after the defi-
nition of FNCT has been closed, the insertion is effected by the
command
[5]
Deletion of a Command
To delete command [4] (i.e., command [3.5] inserted above) of
FNCT , when the system is asking, say, for command [7] of this
function, complete the line by typing [4] and depress the ATTN and
RETURN keys The system will now ask
(in this order). for command
[5] . This number can be overridden by [7] as above.
If the definition of FNCT has already been closed when the need for
110 An Introduction to APL
Depress the ATTN and RETURN keys, in this order, to delete com-
mand [4]
Displays
first of these returns the system to the execution mode after the
requested part of the definition of FNCT has been typed; the second
command leaves the system in definition mode and in expectation of
a change in the last command.
earlier command, illustrate these two kinds of error. Only the second
A
(( 1536/5 )x-/*( "l+2xi 10 )*5)**5
DOMAIN ERROR
((1536/5)x-/*(-i+2x l lo)*5)**5
A
(( 15364 5 )x-/*(~H.2x x 10 )*5)**5
3.14159
112 An Introduction to APL
DOMAIN ERROR
( 1536/5 )xF<— /*(~l + 2xil0)*5)**5
(
A
F
0.996158
Note that for a command containing several errors, these are reported
one at a time. The syntax error reported first is the omission of an
operator (here x ) between two expressions. Other syntax errors are
unmatched parentheses and the use of a function without all the
arguments required by its header.
Note that in the last example, the value of the partial result F is
V+\H
M<-3 3pi9
+7m
CHARACTER ERROR
+
A
VLSI
INDEX ERROR
KC6]
Defined Functions (part 2) 113
LENGTH ERROR
V+Mt 1 ; ]
A
ML 31
RANK ERROR
Ml 3]
A
Will
VALUE ERROR
WL21
A
The next example illustrates a rank error A The variable M has been
defined as a matrix, but the command Ml 3 ] implies that it is a
vector. The final command calls for an undefined value and hence
causes the report of a value error. The attempt to use a function that
is not in the active workspace also produces this kind of error report.
*The term length is here used synonymously with size. See the second footnote
on p. 17.
tThe term rank is here used synonymously with dimensionality. See first the
footnote on p. 17.
114 An Introduction to APL
The first example below again illustrates this situation. As the second
example shows, the same type of error report is made when the name
of a function that is to be defined is the same as the identifier of a
variable already in the active workspace. The third and fourth exam-
ples show that a report of definition error results from incorrect
requests for displays of functions. Note, however, that the choice of
a function name as an identifier of a variable leads to a report of
syntax error.
)FNS
FUNCTN SORT WORDMATRIX
)VARS
BEGIN END
VF+-FUNCTN X
DEFN ERROR
VF+FUNCTN X
VB+BEGIN
DEFN ERROR
VB+BEGIN
VVSORTtODlV
DEFN ERROR
V
A
VS0RTLQU1V
CO] S+N SORT M
VN SORT M CD] V
DEFN ERROR
VN SORT M CD] V
SORT+0
SYNTAX ERROR
SORT+Q
A
1
While one function may invoke a second function, which may in turn
invoke a third function, and so on, excessive length of a chain of this
kind will lead to the report of a depth error.
The report WS FULL indicates that the capacity of the workspace has
been exhausted. After the status indicator has been cleared, the con-
tents of the workspace should be examined and objects no longer
needed should be erased. Similarly, the report SYMBOL TABLE FULL
indicates that too many names are being used. Some functions or
variables should be erased, and the commands
)SAVE CONTINUE
) CLEAR
)COPY CONTINUE
The report RESEND indicates that an error has occurred in the trans-
mission from terminal to computer. The last command should be
repeated. If resend requests occur frequently this fact should be re-
2, . . . .In the body of Table 8.1, the first row contains these func-
"*"
tion values, which will be denoted by f*,f > •ne seconc row '
2
contains the first differences, e/], d\ , . . . , where d^n = f
n +
— fn The .
in exactly the same way in which these are obtained from the func-
tion values, and so on.
The function DIFF shown below takes, as left and right arguments
M and N the order m and the position n of the difference dmn and
, ,
'
TABLE 8.1
Difference Table for f = x 3
n 12 3 4 5 6 7
< 7 19 37 61 91 127
dl 12 18 24 30 36
*n 6 6 6 6
<
VDIFFIQ1V
V D+M DIFF N
[1] ->(m=o)A
[2] ZM(M-l) DIFF(N+1))-(M-1) DIFF N
[3] ->0
[4] D<-FUNC N
V
VFUNCIQ1V
V F+FUNC X
[1] F+X*3
V
2 DIFF 5
36
System Commands
(part 2)
I+-5
)DIGITS I
INCORRECT COMMAND
117
118 An Introduction to APL
)DIGITS 4
) WIDTH 6 5
which the DIGITS command affects the use of the exponent nota-
tion, see Sec. 1.5.
) ORIGIN
WAS 1
This change of index origin, however, may cause trouble if the func-
tion that is being defined invokes other functions that were defined
with index origin 1. For example, with index origin 1, the command
-(0 3)tl+I*Jl
) ORIGIN 1
WAS
Functions
)FNS T
has been listed, we may press the ATTN key to discontinue the
listing.
Library
)LIB 1
C0MPPR0B
AD VAN CEDE X
APLCOURSE
CLASS
NEWS
PLOTFORMAT
TYPEDRILL
WSFNS
)C0PY 1 PLOTFORMAT
SAVED 9.26.53 07/01/70
)FNS
AND DESCRIBE DFT EFT PLOT
System Commands (part 2) 121
HOWPLOT
Note that the function values for x = 3 and x = 4 are 15 and 16, but
the ordinates of the plotted points for these abscissas are equal be-
cause each line of the typed graph corresponds to an ordinate interval
of length 2.
Workspace Identification
Status Indicator
V P-C POLYNOMIAL X
[i] p«-(2, P ;r)po
[2] Ptlll+X
[3] P[2;>Uo.*(pC)-ipC) + .x<7
V
C+l "16 76 "96
C POLYNOMIAL i9
1 3 4 9
35 15 16 21
30
20
o o
10
10
20
30
40 | | I I I
2 10
,
The system commands )SI and )SIV yield the name of each active
function, followed by the appropriate command number, followed
by an asterisk if the function is suspended, and, in the case of )SIV
by a list of the identifiers of all local variables of the function. A
function in this list that has no asterisk is called pendant. Note that
the most recently active function is listed first, and so on.
The values of all local variables of the function at the top of the list
VPRIMESL01V
V PR+PRIMES N;P; e
[1] PR<-li\N
[2] Q+N**P+2
[3] +{Q<P)/Q
[<+] ?/?«-( ( 0*P PR ) vp = p/? ) /Pi?
|
[5] P«-PP[l+P/?iP]
[6] -*3
V
PRIMES 3
2 3 5 7 11 13 17 19 23 2
rf/ItfS 3
2 3 5 11 17 29
SAPRIMES+5
TWINS 3
PRIME SL 5]
)SIT
PRIMESlS] * PR N P «
TWINSL21 T N P
Ports
The command ) PORTS yields a list of the numbers and user codes
of all connected ports (terminals).
The command )P0RT followed by a user code yields only the port
(or ports) for this user code.
System Information
There are some inquiries concerning the system to which the answers
are obtained not by system commands but by special functions. The
name of each of these functions consists of the symbol I (over-
The value of 119 is the total time (in 60ths of a second) during
which the keyboard has been unlocked (i.e., ready to receive entries)
System Commands (part 2) 125
during the current work session. When the R ETU RN key is depressed,
the keyboard is locked and cannot receive new entries until the
present command has been executed (and results have been typed
out if thecommand called for this).
The value of 12 2 is the as yet unused space (in bytes) in the active
workspace. (A byte is equivalent to eight binary digits.) By giving the
command 122 after the active workspace has been cleared, the size
The values of 120 and 124 are the time of the day (since midnight)
and the time of the sign-on for the current work session (in 60ths of
a second).
VDATELDlV
V DATE;D
[1] £>«-(3pl00)Tl25
[2] £[l]; ,
/
,
;£[2];»/ ;£[3]
,
DATE
10/23/70
contents.
sponse to the command, a message is typed out that starts with the
word SAVED and gives the time and date of the last storing of the
workspace.
Note that the LOAD command is destructive in the sense that the
current content of the active workspace is lost. The system command
consisting of the characters )COPY followed by a space and the
name of a stored workspace or the number of a public library, a
space, and the name of a workspace in this library, adds all groups,
functions, and global variables of the named workspace to the cur-
rent contents of the active workspace while retaining the digits,
width, origin, trace, and stop controls of the latter. The response to
the COPY command is the same as that to the LOAD command.
If the COPY command just described is extended by a space and the
name of one object (group, function, or global variable) in the stored
workspace, only this object added to the current content of the
is
active workspace. Note that only one object of the stored workspace
can be copied at a time in this manner.
Note that a function that is being edited and a pendant function can-
not be erased. The name of a stored workspace and its contents may
be dropped from the library by the system command consisting of
the characters )DROP followed by a space and the name of the
stored workspace. The response to the drop command is a line with
the time and date.
Note that the active workspace initially has either no name or the
name CONTINUE depending on whether the previous
, work session
was terminated by )OFF or by ) CONTINUE In the first . case, the
A may have more than one account and may wish to work
user
successively on several of them. To avoid redialing a telephone
128 An Introduction to APL
seconds after the time and cost information for the account has been
typed out, and during this time the user may sign on again by enter-
ing a closing parenthesis and the next account number (and a colon
and password if the account is locked).
APL\360 User's Manual, 2nd ed., White Plains, N. Y.: IBM Technical Publications
Department, 1970.
APL\360 Primer, 2nd ed., White Plains, N. Y.: IBM Technical Publications
Department, 1970.
A Programming Language, by K. E. Iverson, New York: John Wiley & Sons, Inc.,
1962.
129
3
Index
operator, 23 mode, 31
Character: Deletion of command, 109
data, 85, 89 DEPTH ERROR, 115
manipulation, 85 DIFF function, 116
CHARACTER ERROR, 113 Difference table, 115
Checking function definition, 43, 105 DIGITS command, 6, 49, 117
CLEAR command, 52 Dimension (see Size)
CODE, function, 96 Dimensionality of array, 17, 63
131
132 Index
composite, 8, 10
multiple evaluation of, 13 Label, 104
Language translation, automatic, 97
Factorial, 10 Length {see Size)
FNS command, 50, 119 LENGTH ERROR, 113
Function: LIB command, 120
pendant, 123 Library:
suspended, 46, 121 adding to a, 52
Function definition, editing of, 32, 108 starting a, 49
Fuzz, 27 Library control commands, 125
LOAD command, 125
Gamma function, 10
Local variables, 41, 123
Global variables, 41 Logarithm, 10, 55
Grade down, 75
Grade up, 75 Matrix, 17
GROUP command, 50 Matrix product, 26, 69
GROWTH, function, 31,32, 113 Membership in array, 27
GROWTHTABLE, function, 34,41, Monadic operators, 9, 1 1, 23
72, 102
GRP command, 120 OFF command, 4
GRPS command, 51, 119, 120
OFF HOLD command, 128
Operations on character data, 89
Headline, 31 Operator:
types, 101 catenation, 23
Index 133
WORDS, function, 92
Workspace, 49 ZERO, function, 38
^
The Ally n and Bacon Series
MU?
LANGUAGES
OF THE 70s
Consulting Editor
Peter Wegner
Brown University
C. Joseph Sass
BASIC Programming For Business
RELATED TITLES
An Introduction to
Computer Science and Algorithmic Proce
Terry M. Walker and William W.
Cotterman
Elementary Numerical Analysis With
Programming
Gerald B. Haggerty
203222