In APL the
precedence hierarchy for functions or operators is strictly positional: expressions are evaluated right-to-left. APL does not follow the usual
operator precedence of other programming languages; for example, × does not bind its operands any more "tightly" than +. Instead of operator precedence, APL defines a notion of
scope. The
scope of a
function determines its
arguments. Functions have
long right scope: that is, they take as right arguments everything to their right. A dyadic function has
short left scope: it takes as its left arguments the first piece of data to its left. For example, (leftmost column below is actual
program code from an APL
user session, indented = actual
user input, not-indented = result returned by
APL interpreter): 1 ÷ 2 ⌊ 3 × 4 - 5 ¯0.3333333333 1 ÷ 2 ⌊ 3 × ¯1 ¯0.3333333333 1 ÷ 2 ⌊ ¯3 ¯0.3333333333 1 ÷ ¯3 ¯0.3333333333 APL is going to execute from right-to-left. Step 1{of topmost APL code entered at left}) 4-5 = -1. Step 2) 3 times -1 = -3. Step 3) Take the
floor or
lower of 2 and -3 = -3. Step 4) Divide 1 by -3 = -0.3333333333 = final result. An operator may have function or data
operands and evaluate to a dyadic or monadic function. Operators have long left scope. An operator takes as its left operand the longest function to its left. For example: ∘.=/⍳¨3 3 1 0 0 0 1 0 0 0 1 APL
atomic or piecemeal sub-analysis (
full explanation): Beginning rightmost: ⍳¨3 3 produces a 2-element nested APL vector { {1 2 3} {1 2 3} } where each element is itself a vector {1 2 3}.
Iota ⍳3 by itself would produce {1 2 3}. The
diaeresis ¨ or mini double-dot means
repeat or
over each or
perform each separately so
iota repeats (in human i.e., reversed terms, the APL interpreter reads 3 3 over each use iota), concisely:
iota for each 3. The left operand for the
over-each operator ¨ is the
index ⍳ function. The
derived function ⍳¨ is used monadically and takes as its right operand the vector 3 3. The left scope of
each is terminated by the
reduce operator, denoted by the forward
slash. Its left operand is the function expression to its left: the
outer product of the
equals function. The result of ∘.=/ is a monadic function. With a function's usual long right scope, it takes as its right argument the result of ⍳¨3 3. Thus
(⍳3)(⍳3) 1 2 3 1 2 3
(⍳3)∘.=⍳3 1 0 0 0 1 0 0 0 1 1 2 3 1 2 3
∘.=/⍳¨3 3 1 0 0 0 1 0 0 0 1 Equivalent results in APL:
(⍳3)(⍳3) and '
∘.=/⍳¨3 3 and (⍳3)∘.=⍳3' is called an
identity matrix. Identity matrices are useful in solving
matrix determinants, groups of
linear equations and
multiple regression. im ← ∘.=⍨∘⍳ im 3 1 0 0 0 1 0 0 0 1 Some APL interpreters support the
compose operator ∘ and the
commute operator ⍨. The former
∘ glues functions together so that
foo∘bar, for example, could be a hypothetical function that applies defined function
foo to the result of defined function
bar; foo and bar can represent
any existing function. In cases where a dyadic function is moderated by
commute and then used monadically, its right argument is taken as its left argument as well. Thus, a
derived or
composed function (named
im at left) is used in the APL user session to return a 9-
element identity matrix using its right
argument,
parameter or operand = 3. Letters←"ABCDE" Letters ABCDE ⍴Letters 5 FindIt←"CABS" FindIt CABS ⍴FindIt 4 Letters ⍳ FindIt 3 1 2 6 Example using APL to
index ⍳ or
find (or not find) elements in a character vector: First, variable
Letters is assigned a vector of 5-elements, in this case - letters of the alphabet. The
shape ⍴ or character vector-length of
Letters is 5. Variable
FindIt is assigned what to
search for in
Letters and its length is 4 characters. 1 2 3 4 5 ABCDE At left, dyadic function
iota searches through its left argument(Letters) for the search string (iota's right argument, FindIt). Iota finds letter "C" at position 3 in Letters, it finds "A" at position 1, and "B" at position 2.
Iota does
not find letter "S" anywhere in variable Letters so it returns the number 6 which is
1 greater than the length of Letters.
Iota found letters "CAB" (3 1 2).
Iota correctly did
not find "S" (6). ==Monadic functions==