A direct function (dfn, pronounced "dee fun") is an alternative way to define a function and operator (a higher-order function) in the programming language APL. A direct operator can also be called a dop (pronounced "dee op"). They were invented by John Scholes in 1996. They are a unique combination of array programming, higher-order function, and functional programming, and are a major distinguishing advance of early 21st century APL over prior versions. A dfn is a sequence of possibly guarded expressions (or just a guard) between { and }, separated by ⋄ or new-lines, wherein ⍺ denotes the left argument and ⍵ the right, and ∇ denotes recursion (function self-reference). For example, the function PT tests whether each row of ⍵ is a Pythagorean triplet (by testing whether the sum of squares equals twice the square of the maximum).
The factorial function as a dfn:
Description The rules for dfns are summarized by the following "reference card":
A dfn is a sequence of possibly guarded expressions (or just a guard) between { and }, separated by ⋄ or new-lines.
The expressions and/or guards are evaluated in sequence. A guard must evaluate to a 0 or 1; its associated expression is evaluated if the value is 1. A dfn terminates after the first unguarded expression which does not end in assignment, or after the first guarded expression whose guard evaluates to 1, or if there are no more expressions. The result of a dfn is that of the last evaluated expression. If that last evaluated expression ends in assignment, the result is "shy"—not automatically displayed in the session. Names assigned in a dfn are local by default, with lexical scope. ⍺ denotes the left function argument and ⍵ the right; ⍺⍺ denotes the left operand and ⍵⍵ the right. If ⍵⍵ occurs in the definition, then the dfn is a dyadic operator; if only ⍺⍺ occurs but not ⍵⍵, then it is a monadic operator; if neither ⍺⍺ or ⍵⍵ occurs, then the dfn is a function. The special syntax ⍺←expression is used to give a default value to the left argument if a dfn is called monadically, that is, called with no left argument. The ⍺←expression is not evaluated otherwise. ∇ denotes recursion or self-reference by the function, and ∇∇ denotes self-reference by the operator. Such denotation permits anonymous recursion. Error trapping is provided through error-guards, errnums::expression. When an error is generated, the system searches dynamically through the calling functions for an error-guard that matches the error. If one is found, the execution environment is unwound to its state immediately prior to the error-guard's execution and the associated expression of the error-guard is evaluated as the result of the dfn. Additional descriptions, explanations, and tutorials on dfns are available in the cited articles.
Examples The examples here illustrate different aspects of dfns. Additional examples are found in the cited articles.
Default left argument The function {⍺+0j1×⍵} adds ⍺ to 0j1 (i or −1) times ⍵.
The significance of this function can be seen as follows:
Complex numbers can be constructed as ordered pairs of real numbers, similar to how integers can be constructed as ordered pairs of natural numbers and rational numbers as ordered pairs of integers. For complex numbers, {⍺+0j1×⍵} plays the same role as - for integers and ÷ for rational numbers. Moreover, analogous to that monadic -⍵ ⇔ 0-⍵ (negate) and monadic ÷⍵ ⇔ 1÷⍵ (reciprocal), a monadic definition of the function is useful, effected by specifying a default value of 0 for ⍺: if j←{⍺←0 ⋄ ⍺+0j1×⍵}, then j ⍵ ⇔ 0 j ⍵ ⇔ 0+0j1×⍵.
The last expression illustrates Euler's formula on ten random numbers with real and imaginary parts in the interval (−0.5,0.5).
Single recursion The ternary construction of the Cantor set starts with the interval [0,1] and at each stage removes the middle third from each remaining subinterval: [0,1]→ [0,13]∪[23,1]→ [0,19]∪[29,13]∪[23,79]∪[89,1]→⋯ The Cantor set of order ⍵ defined as a dfn:
Cantor 0 to Cantor 6 depicted as black bars:
The function sieve ⍵ computes a bit vector of length ⍵ so that bit i (for 0≤i and i<⍵) is 1 if and only if i is a prime.
The last sequence, the number of primes less than powers of 10, is an initial segment of OEIS: A006880. The last number, 50847534, is the number of primes less than 109. It is called Bertelsen's number, memorably described by MathWorld as "an erroneous name erroneously given the erroneous value of π(109)=50847478". sieve uses two different methods to mark composites with 0s, both effected using local anonymous dfns: The first uses the sieve of Eratosthenes on an initial mask of 1 and a prefix of the primes 2 3...43, using the insert operator ⌿ (right fold). (The length of the prefix obtains by comparison with the primorial function ×⍀p.) The second finds the smallest new prime q remaining in b (q←b⍳1), and sets to 0 bit q itself and bits at q times the numbers at remaining 1 bits in an initial segment of b (⍸b↑⍨⌈n÷q). This second dfn uses tail recursion.
Tail recursion Typically, the factorial function is define recursively (as above), but it can be coded to exploit tail recursion by using an accumulator left argument:
Similarly, the determinant of a square complex matrix using Gaussian elimination can be computed with tail recursion:
Multiple recursion A partition of a non-negative integer n is a vector v of positive integers such that n = +⌿v, where the order in v is not significant. For example, 2 2 and 2 1 1 are partitions of 4, and 2 1 1 and 1 2 1 and 1 1 2 are considered to be the same partition. The partition function P(n) counts the number of partitions. The function is of interest in number theory, studied by Euler, Hardy, Ramanujan, Erdős, and others. The recurrence relation
P(n)=∑k=1n(−1)k+1[P(n−12k(3k−1))+P(n−12k(3k+1))] derived from Euler's pentagonal number theorem. Written as a dfn:
The basis step 1≥⍵:0≤⍵ states that for 1≥⍵, the result of the function is 0≤⍵, 1 if ⍵ is 0 or 1 and 0 otherwise. The recursive step is highly multiply recursive. For example, pn 200 would result in the function being applied to each element of rec 200, which are:
and pn 200 requires longer than the age of the universe to compute (7.57×1047 function calls to itself). The compute time can be reduced by memoization, here implemented as the direct operator (higher-order function) M:
… excerpt ends here. Continue reading the full article.
