APL

Table of Contents

1. Declaration

  • : Assign
    • Expressions are executed, Definitions are remembered.
  • String is a vector of character. It is surrounded by single quotes. e.g. 'APL'

2. Array

  • Every data is array.
  • Each entry can be either number, character or array.
    • integer or float are automatically dealt with.
  • Array can be called scalar, vector or matrix depending on their shape.
  • array[index[;index]*] to access its entries. index [index]* ⌷ array is also available.

2.1. Depth

  • It is the level of nesting.
  • : Monadic, Negative if the array is not even.

2.2. Rank

  • It is the shape of the array. It does not consider the shape of its entries.
  • Scalar has the shape of empty vector .
    • Scalar can contain an array which is called the enclosed vector or nested scalars.
  • : Return the shape if monadic, Reshape if dyadic.
    • It repeats the entries when the shape is larger, truncates if the shape is smaller.

3. Function

  • It takes everything around it as its arguments, evaluated from the left.
  • Monadic function takes from the right, and dyadic functions take from both sides. Those are the only two options.
  • Scalar function pairs (or array extends if one of its argument is scalar) and then penetrate.
  • Mixed functions are all other functions that work on their own way.
  • Dfns (dynamic-function) is an inline anonymous function. It is enclosed within braces.

3.1. Tacit Function

  • Tacit function (aka train) is a statement that does not have data on the right.
  • It is defined without the braces.

3.1.1. Two-Train

  • atop
    • Function composition
    • (f g) X = f (g X)
    • X (f g) Y = f (X g Y)
      • where g is dyadic.
    • : Atop. f⍤g does the same thing.

3.1.2. Three-Train

  • f g h fork
    • (f g h) X = (f X) g (h X)
    • X (f g h) Y = (X f Y) g (X h Y)
      • where f and g are dyadic.
  • A g h fork
    • A is an array.
    • (A g h) X = A g (h X)
    • X (A g h) Y = A g (X h Y)
      • where h is dyadic.
  • and  return left argument and right argument respectively. When used in a monadic function, both return the right argument.
  • If the train is longer than three, then the last three become the three train recursively.

3.2. Built-In

  • : Match.
  • : Tally, Not Match. The length of the leading axis.
  • +: Add (Complex conjugate), -: Subtract (0-), ×: Multiply (Normalize), ÷: Divide (Reciprocal), *: Exponentiate (e*), : Logarithm (e⍟), : Max (Ceiling), : Min (Floor), |: Residue (Magnitude).
  • : And, lcm. : Or, gcd. , : NAND, NOR, ~: Not.
  • =, , <, >, , : Dyadic. Comparison. Scalar Function. Return 0 or 1.
  • , : Grade Up/Down. Return the indexes in sorted order. The order can be given when used dyadic.
  • : Mix. Monadic. Nest -> Rank. Filled with the type of first element of each nested array. : Split. Monadic. Rank -> Nest. : Enclose. Enclose an array in a scalar. Simple scalar cannot be enclosed. : Disclose. Take the first element.
  • ,: Catenate, Laminate.
  • : Reverse. : Reverse, but along the first axis.
  • : Transpose.

4. Operator

  • /: Reduce. The result is enclosed. It is evaluated from the last two elements along the last axis, since APL is right-associative.
  • : Reduce, but along the first axis.
  • \: Scan. Calculate accumulations from the first element.
  • : Scan, but along the first axis.
    • L f/[N] R: L specifies the width of windows, and N specifies the index of the axis.
    • A / B filters (replicates) B with A.
  • ¨: Each.
  • : Self, Swap. Duplicate the argument or swap the arguments.
  • : Repeat. f⍣n repeat f n times. n can be negative for the inverse operation, or = to repeat until the fixed point is achieved.
  • . Dot: Inner Product. f.g apply g to each and reduce with f. +.× Vector Dot Product, Matrix Multiplication.
  • Jot: Bind, Beside. d∘m = ⍺ d (m ⍵), D combinator. x∘d or d∘x bind x to the left or right of the dyadic function turning it into a monadic one.
  • ∘.: Outer Product. ∘.g takes the outer product with g.
  • : Rank. Set the rank for the next operator to work on. Atop, the two train.
  • : Over. d⍥m Apply monadic function to both of the arguments before applying the dyadic one. S' Combinator
  • @: At. (m|x)@(A|m) apply m or set to x, if in A or m evaluates to 1.

5. Others

  • : Comment.
  • : End of Expression
  • ... : ... ⋄ ...: Ternary. Only works within dfn

6. See Also

Created: 2025-05-06 Tue 23:25