## User-defined Operators

An 'operator' modifies the behaviour of a primitive or user-defined function. It has an operand or operands that are primitive, derived or user-defined functions or data. The result of using an operator is known as a derived function which can then be applied monadically or dyadically to data or alternatively it may be, in turn, used as an argument to another operator.

As well as the primitive (built-in) operators, user-defined operators are permitted. These are created an edited in the same way as user-defined functions (using the ’ editor, or )EDIT), but are distinguished from functions by the format of the header, line 0.

### Header line for user-defined operators

The format for an operator header follows one of the following forms, where LO=Left operand, RO=Right operand, A=Left argument, B=Right argument:

                 R„(LO OPERATOR) B          (Monadic operator with one argument)
    or
                 R„A (LO OPERATOR) B        (Monadic operator with two arguments)
    or
                 R„(LO OPERATOR RO) B       (Dyadic operator with one argument)
    or
                 R„A (LO OPERATOR RO) B     (Dyadic operator with two arguments)

User-defined operators need not return explicit results.

### Example

This simple monadic operator with two arguments `COMMUTE` reverses the arguments of a function. In this example, `FN` represents the function (the left operand) which will be combined with the operator to make a derived function, `L` represents the left argument supplied to the derived function, and `R` represents the right argument supplied to the derived function:

          ’Z„L (FN COMMUTE) R
    [1]  © Operator which reverses the arguments to a dyadic function
    [2]   Z„R FN L
          ’

          100 ÷ 3
    33.33333333
          100 ÷ COMMUTE 3       © Equivalent to 3 ÷ 100
    0.03
          100 ½ COMMUTE 3       © Equivalent to 3 ½ 100
    100 100 100
          100 ŒDR COMMUTE 1     © Equivalent to 1 ŒDR 100
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0

### Using data as operands

The left and/or right operands to a user-defined operator do not have to be functions; they can alternatively be arrays. The effect is to substitute the supplied array for in expression which references the operand:

          'FRUIT' ('OLD' COMMUTE) 'HELLO'
    HELLO OLD FRUIT

Topic: APLX Help : Help on APL language : APL Fundamentals : Operators

[ Previous | Next | Contents | Index ]