Binding Strengths (7 of 9)

In simple terms, APL evaluates expressions right-to-left, that is to say the result of the rightmost function is evaluated, and becomes the right argument of the next function. There are no 'precedence rules' to remember; all primitive and user-defined functions have the same precedence. For example:

      5⍴3.2×12÷4
9.6 9.6 9.6 9.6 9.6

In this example, the division 12÷4 is evaluated first. The result of this expression becomes the right argument of the multiply (×), which returns the scalar result 9.6. This in turn becomes the right argument of the reshape () function.

The right to left function execution rule needs to be modified to cope with more complex expressions, for example nested vectors or certain expressions containing operators. The 'binding strength' defines how certain symbols 'bind' for evaluation. The order of binding strengths is shown below, in descending order.

Binding

Bound items

Brackets []

Brackets to object to the left

Specification left

to object on its left

Right Operand

Dyadic operator to its right operand

Vector

Array to array

Left Operand

Operator to its left operand

Left Argument

Function to left argument

Right Argument

Function to right argument

Specification right

to object on its right

Parentheses can override the binding strength hierarchy. Some examples include:

             A←'DEF'                 (Set up variables A B)
             B←'XYZ'
             A B
       DEF XYZ
             A B[2]                  ([] has higher binding than vector so the
       DEF Y                          result includes the second element of B)
             (A B)[2]                (Parentheses force selection of B)
       XYZ
             A B←3                   (← has stronger binding than vector)
       DEF 3
             (A B)←3                 (Parentheses alter binding)
             A
       3
             B
       3
             1 2 3 + 4 5 6           (Vector has stronger binding than function)
       5 7 9
             1 2 (3+4) 5 6           (Parentheses alter binding)
       1 2 7 5 6
             1 0 1/'ABC'             (Vector has stronger binding than left
       AC                             operand, so left operand is 1 0 1)
             +/[2]2 2⍴⍳4             (Axis brackets have stronger binding than
       3 7                            operator to operands, so /[2] operator
                                      is formed

Finally, the relative binding strengths of left and right operands can be used to predict the result of expressions with multiple operators. +.×.- is evaluated as (+.×).- and not as +.(×.-) since the × binds first as right operand to the first . (inner product) operator.


CategoryAboutApl

LearnMoreApl/BindingStrengths (last edited 2017-02-16 18:50:16 by KaiJaeger)