Builtin Functions 

APL provides 50 or so builtin functions that do all the conventional operations and some rather sophisticated ones as well. It also provides 5 operators that modify and extend the way the functions work. These functions and operators can be used together very flexibly. Consequently it's possible to do a great deal in APL without ever leaving calculator mode: one APL line can be the equivalent of an entire subroutine in another language. ArgumentsMost APL functions are capable of doing two different but related tasks. The number of 'arguments' the function is given to work on determines which task it will perform. Here's the ⌈12.625 13 2 ⌈ 8 8 In the first (monadic or oneargument) form, the function rounded up the number on its right to the next whole number. In the second (dyadic or twoargument) form it selected the bigger of the two numbers. Here's another example: ÷1 2 3 4 5 1 0.5 0.3333333333 0.25 0.2 100÷1 2 3 4 5 100 50 33.33333333 25 20 In the first example the function Execution orderA line of APL may consist of several functions, and arguments. There are therefore two points you must bear in mind:
This simple example illustrates both of these points: 50×21 50 The expression is evaluated from right to left so Numbers or textSome functions work on numbers only. The arithmetic functions are in this category. You'll get a message saying you've made a Some functions work on either. The The logical functions (logical Shape and size of dataSome functions can be used only on data of a certain shape. Matrix divide ( Any two data items involved in arithmetic and certain other operations must, however conform in size and shape, that is, it must be possible for APL to match each element in the lefthand argument with each equivalent element in the righthand argument. The arguments in this example don't match and an error results: 29 51 60 27÷3 11 LENGTH ERROR 29 51 60 27÷3 11 ^ The reasonable exception to this rule is the case where one argument consists of a single element and the other argument consists of a vector, matrix or multidimensional array: 39 5 91×2 78 10 182 APL simply applies the single element to each element in the other argument. There's no ambiguity about which element matches which, and no error results. Groups of functionsIn the rest of this chapter we take groups of functions that do related things and discuss briefly what each group does. A few examples are given, partly to illustrate particular functions and partly to 'acclimatise' you to APL. To find out more about any function, see the APLX Language Manual which provides a definition and examples for each function. The groupings chosen are:
Arithmetic functionsAll the usual arithmetic operations are provided. What makes APL arithmetic interesting is not the functions themselves, but the fact that they can be applied to simple or complicated data. A multiplication, for example can take place between two numbers. Alternatively every number in one matrix can be multiplied by the corresponding number in a second matrix producing a third matrix of the products. (The scope of operations is extended further by the APL operators, but they are dealt with in the next chapter.) Remember that each function is capable of two different operations, depending on whether it's used with one or two arguments. The different operations are identified in the table below.
(Note: the Examples of arithmetic functions 1. A vector of numbers is multiplied by a single number. 2 6 3 19 × 0.5 1 3 1.5 9.5 2. A vector of numbers is divided by a single number: 3 7 8 11 ÷ 3 1 2.333333333 2.666666667 3.666666667 3. A vector of numbers is divided by a single number. The results are rounded up to the next whole number and are then displayed: ⌈ 3 7 8 11 ÷3 1 3 3 4 4. The same operation as the last example, except that ⌈ ¯0.5 + 3 7 8 11 ÷3 1 2 3 4 5. Two vectors containing some negative values are added. ×12 ¯1 3 ¯5 + 2 ¯6 ¯4 5 1 ¯1 ¯1 0 6. The remainder of dividing 17 into 23 is displayed: 17  23 6 7. The remainders of two division operations are compared and the smaller of the two is displayed as final result: (3  7 ) ⌊ 4  11 1 Algebraic functionsThese are functions for doing more advanced arithmetic. The Some functions have more everyday uses. The The
Examples of algebraic functions 1. The numbers 1 to 10 are put in a variable called X ← ⍳10 1 2 3 4 5 6 7 8 9 10 2. 3 random numbers between 1 and 10, with no repetitions. 3?10 2 8 3 3. The logarithm to the base 2 of 2 4 8. 2 ⍟ 2 4 8 1 2 3 4. The number of combinations of 2 items which can be made from a population of 4 items. 2 ! 4 6 Comparative functionsComparative functions naturally take two arguments. The arguments are compared and if the condition specified by the function ('less than', 'equal to' or whatever) is true, the result is Comparative functions are useful for finding items of data that meet certain conditions (e.g. employees whose sex equals
Examples of comparative functions 1. Are two given numbers equal? (1 = yes 0 = no) 10 = 5 0 12 = 12 1 2. Are the corresponding characters in two strings equal? 'ABC' = 'CBA' 010 3. Is the first number greater than the second? 10 > 5 1 4. Is each number in the first vector less than the matching number in the second vector? 3 9 6 < 9 9 9 101 5. Is the number on the left in the vector on the right? 12 ∊ 6 12 24 1 6. Is the character on the left in the string on the right? 'B' ∊ 'ABCDE' 1 7. Which numbers in a matrix are negative? (The contents of TABLE 12 54 1 ¯3 90 23 16 ¯9 2 TABLE < 0 0 0 0 1 0 0 0 1 0 8. Find the number on the right in the vector on the left and show its position. 13 7 9 0⍳9 3 9. Are two matrices exact matches? (2 2⍴⍳4) ≡ (2 2⍴⍳4) 1 10. Find the pattern 'CAT ' ⍷ 'THATCAT ' 0 0 0 0 1 0 0 Logical functionsThese are also comparative functions, but they work with data composed exclusively of For example, if you've used the comparative function Logical comparisons are also used in userdefined functions to control branching.
Examples of logical comparison functions 1. ~1 1 1 0 0 0 1 0 0 0 1 1 1 0 2. The same data submitted to various logical functions: 1 ∨ 0 1 1 ^ 0 0 1 ⍱ 0 0 1 ⍲ 0 1 3. Each element in one vector is compared ( 1 0 1 ^ 0 0 1 0 0 1 4. Two expressions are evaluated. If both are true (i.e. both return a value of 1) then the whole statement is true (i.e. returns a value of 1): (5 > 4) ^ 1 < 3 1 Manipulative functionsThese functions do a variety of useful operations on data. The one argument form of
The
Examples of manipulative functions 1. An enquiry about the size of a character string: ⍴ 'ARLINGTON A.J, 22 BOND RD SPE 32E' 33 2. A threerow fourcolumn matrix is formed from the numbers 1 to 12 and is assigned to DOZEN ← 3 4 ⍴ ⍳ 12 DOZEN 1 2 3 4 5 6 7 8 9 10 11 12 3. The matrix ,DOZEN 1 2 3 4 5 6 7 8 9 10 11 12 4. The matrix (,DOZEN), 13 14 15 DOZEN 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 5. The matrix DOZEN ← 3 4⍴⌽,DOZEN 12 11 10 9 8 7 6 5 4 3 2 1 6. Even numbers are removed from a vector. 1 2 3 4 5 6 ~ 2 4 6 1 3 5 7. First 3 characters are selected from a vector: 3 ↑'AWFULLY' AWF 8. An array is made into a nested scalar. ⊂999 34 999 34 ⍴⊂999 34 empty The two element numeric vector is 'enclosed' into a single nested scalar, with an empty shape. Sorting and coding functionsThe sorting functions enable you to arrange data in ascending or descending order. You can either use the computer's collating sequence to determine sorting order, or you can specify your own collation sequence. When looking at the examples of The coding functions
Examples of sorting and coding functions 1. To put a vector of numbers into ascending order: LIST ← 200 54 13 9 55 100 14 82 ⍋LIST 4 3 7 2 5 8 6 1 LIST[4 3 7 2 5 8 6 1) 9 13 14 54 55 82 100 200 2. To sort the same vector as in example 1 with less typing: LIST[⍋LIST] 9 13 14 54 55 82 100 200 3. To find how certain symbols rank in the collating order (i.e. the order in which APL holds characters internally): SYMBS ← '"\;,./' ORDER ← ⍋SYMBS SYMBS[ORDER] ,./;\" 4. To convert the hex number 21 to its decimal equivalent: 16 16 ⊥ 2 1 33 Miscellaneous functions and other symbolsMany of these involve the input of data from the keyboard, or affect the way data is displayed or printed.
Examples of miscellaneous symbols 1. To carry out more than one APL statement on a single line, then to format the data (turn a number into its character equivalent) and make up a single character vector using the catenate function: 'TYPE A NUMBER' ⋄ NUM ← ⎕ 'YOU TYPED', ⍕NUM 2. To display each number in a vector in a 6character field with two decimal places: 6 2 ⍕ 60.333333 19 2 52.78 60.33 19.00 2.00 52.78 3. To display each number in a vector preceded by a dollar sign and with up to three leading zeroes suppressed: '$$Z,ZZ9' ⍺ 3899 66 2 $3,899 $66 $2 4. To index the third item from a vector: 3 ⌷ 1 2 3 4 5 3 (The miscellaneous symbols are treated in detail in the APLX Language Manual) System functionsAnother very powerful type of builtin function in the System Function. This is a large topic which is introduced in the next chapter.

Copyright © 19962008 MicroAPL Ltd