• Aucun résultat trouvé

Arrays and Functions

Dans le document APL/PC Version 2.1 (Page 65-69)

Let us look in more detail at each of the terms used above.

An array can be as simple as a single number or single character, and is then called a scalar. If it is a simple

one-dimensional list, it is called a vector. A two-dimensional table is called a matrix. No special name exists for arrays with.

three or more dimensions. The number of dimensions of an array is called its rank, and each dimension is called an axis.

An array must be homogeneous; that is, it must contain only numbers or only characters. Characters can be chosen from the 256 ASCI I characters, including: letters, digits,

punctuation marks, special characters, and control characters such as line-feed. Numbers can be whole integers, or may have fractional parts or exponents: in the terminology of other computer languages, APL numbers may be either fixed or floating point, at will. Boolean arrays (values of 0 and 1, which are also truth values in APL) are packed together by 2-4 APL/PC, Version 2.1

~M Internal Use Only

PL to save storage, but for computational purposes, they are eated like any other numbers.

n array with more than one dimension must be rectangular:

lat is, all parallel axes must contain the same number of data .ements. For example, it is not permissible in APL to have a latrix with a different number of items in each row.

'unctions come in several varieties. Most important are the rimitive built-in functions, which perform arithmetic,

~lational, structural and general computing operations. For xample:

"+" does addition

"x" does multiplication

"*" does exponentiation

"~" computes the logarithm

" ! " computes the factorial (Gamma) function

"v" computes the boolean "or"

" r"

finds the maximum

" E" de~ermines membership in a set,

" 1" searches a vector for a given item

• "Ii" determines the alphabetic or numeric order of an array

• " ?" generates random integers

• "T" converts to any desired number base

• "m"

inverts a matrix

The primitive structural functions are unique to APL among the widely used pro gramming languages. They include:

Chapter 2. APL Tutorial 2-5

IBM Internal Use On

• catenation (using comma, " I")

• determining the current shape of a data array (monadic rho, "p")

• changing the shape of an array of data (dyadic rho, "p")

• extraction of the first (or last) few columns of a matrix or items of a vector (take, "1''')

• dropping of the first (or last) few columns or items (drop,

"", ")

• transposition (transpose, "~")

• rotation and reflection (rotate, "$")

• extraction of an arbitrary rectangular subarray using vecto subscripts (with" [] ")

• extraction of an arbitrary rectangular subarray according to a boolean relationship (such as "(C;t; r r )/C")

There are nearly sixty primitive functions in APL, each one symbolised by a single special character. With some

exceptions, the primitive functions can operate on arrays of any size or rank. Similarly, the non-arithmetic and structural functions work on numeric or character arrays in similar ways.

Other kinds of functions may be created by the user of APL as needed. Derived functions are written as two or three adjacent special characters, and represent a compound of one or two primitive functions with a primitive operator. For example, the built-in operator" /" is called reduction and means "combine all items along one axis according to the indicated function".

"+ / 6 7 2" computes the sum 15 of the vector shown, "x / 6 7 2" computes the product 8 4, and

"r /

6 7 2" computes the maximum 7. The inner product operator, represented by a period, combines two functions in a particular way. For example, "A + • xB" is the familiar "dot product" of matrix algebra.

2-6 APL/PC, Version 2.1

IBM Internal Use Only

Lastly, defined, or user-written functions correspond to the subroutines or procedures of other languages, and may contain many lines of code. They are generally written with the aid of afunction editor. Only one control structure is permitted in defined functions - the branch (or "go-to"), symbolised by an arrow pointing to the right (-+). Loops can be written

explicitly with branches, but in practice, they are required much less often in APL than in other programming languages because of the built-in array functions and operators.

Note: The terms function and operator refer to two very different kinds of object in APL. Functions work on data arrays to produce new data arrays, while operators work on functions to produce new functions. In other computer languages, these terms are often used interchangeably.

Interactivity

Normally, all lines of APL must be entered from the keyboard.

However, any line may invoke user-defined functions, so often all the user need type is a brief "trigger" expression to start a long computation running. It is possible with the aid of Auxiliary Processor 101 (APlOl) to automate responses that would otherwise have to be typed in manually to a program that requests them.

To write a defined function, a function editor is needed. APL provides one built-in, called the del-editor, after the special character "\J", which causes entry to it. The body of a defined function consists of APL lines to be executed in order, plus branch statements to alter the order of execution. The header line gives the name of the defined function, and the names of the dummy variables which are the input, output and local work variables. (Other function editors are available too;

workspace ED IT has one written in APL, and another that can invoke a DOS editor such as Personal Editor).

Testing a defined function is as simple as typing in a line of APL code that executes it. A natural style of writing a complex program in APL is to write and test subcomponents separately. Test data for the subcomponents can be

pre-computed or entered by hand, and kept together with the

Chapter 2. APL Tutorial 2-7

IBM Intcinal Use Only

function being tested in the workspace. It is never necessary to write a complete function in APL before testing it. This procedure greatly decreases the time needed to produce a working program.

When an error occurs in a line of APL code, the APL system stops immediately and displays an error message along with a copy of the offending statement and a pointer to the error position. Often, the user can immediately determine the source of the error, correct it, and resume execution from the point of error. For exarnple, suppose that inside a defined function narned COMPUTE, line 4 performs a division by variable X.

Should X happen to be 0 at some time, then execution of that line would produce the following display:

DOMA.IN ERROR COMPUTE [4]

Z+l+X

A

The execution of function COMPUTE is now suspended, and the APL system is waiting for the user to type in a new conunand.

The user can determine that X is indeed 0, and then perhaps might set X to another value and resume execution at this same line to see what other errors show up. Eventually, the user may abandon the current execution altogether, correct all the errors discovered in the defined functions, and restart. For more details see the section "Debugging" on page 2-28.

Dans le document APL/PC Version 2.1 (Page 65-69)

Documents relatifs