Direct function

A direct operator can also be called a dop (pronounced "dee op").

The factorial function as a dfn: The rules for dfns are summarized by the following "reference card":[2] A dfn is a sequence of possibly guarded expressions (or just a guard) between { and }, separated by ⋄ or new-lines.

If that last evaluated expression ends in assignment, the result is "shy"—not automatically displayed in the session.

The last expression illustrates Euler's formula on ten random numbers with real and imaginary parts in the interval

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:

[10]: §46 The last sequence, the number of primes less than powers of 10, is an initial segment of OEIS: A006880.

The function is of interest in number theory, studied by Euler, Hardy, Ramanujan, Erdős, and others.

The recurrence relation derived from Euler's pentagonal number theorem.

[15] Written as a dfn:[10]: §16 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.

[16] The memo operator M defines a variant of its operand function ⍺⍺ to use a cache T and then evaluates it.

The three parts generated at each recursive step are apparent in the structure of the final result.

Applying the function derived from Q3 to the same argument multiple times gives different results because the pivots are chosen at random.

The above formulation is not new; see for example Figure 3.7 of the classic The Design and Analysis of Computer Algorithms.

[17] However, unlike the pidgin ALGOL program in Figure 3.7, Q is executable, and the partial order used in the sorting is an operand, the (×-) the examples above.

The following snippet solves a "Programming Pearls" puzzle:[18] given a dictionary of English words, here represented as the character matrix a, find all sets of anagrams.

[9]: §3.3  The expression on the right is a train, a syntactic form employed by APL to achieve tacit programming.

When an inner (nested) dfn refers to a name, it is sought by looking outward through enclosing dfns rather than down the call stack.

The distinction becomes apparent only if a call is made to a function defined at an outer level.

The example shows the unwinding of the local environment before an error-guard's expression is evaluated.

The local name tx is set to describe the purview of its following error-guard.

When an error occurs, the environment is unwound to expose tx's statically correct value.

Kenneth E. Iverson, the inventor of APL, was dissatisfied with the way user functions (tradfns) were defined.

Examples of using direct definition are found in the 1979 Turing Award Lecture[21] and in books and application papers.

Of these, the "alternative APL function definition" of Bunda in 1987[31] came closest to current facilities, but is flawed in conflicts with existing symbols and in error handling which would have caused practical difficulties, and was never implemented.

The main distillates from the different proposals were that (a) the function being defined is anonymous, with subsequent naming (if required) being effected by assignment; (b) the function is denoted by a symbol and thereby enables anonymous recursion.

[9] In 1996, John Scholes of Dyalog Limited invented direct functions (dfns).

[1][6][7] The ideas originated in 1989 when he read a special issue of The Computer Journal on functional programming.

[33] He then proceeded to study functional programming and became strongly motivated ("sick with desire", like Yeats) to bring these ideas to APL.

[6][7] He initially operated in stealth because he was concerned the changes might be judged too radical and an unnecessary complication of the language; other observers say that he operated in stealth because Dyalog colleagues were not so enamored and thought he was wasting his time and causing trouble for people.

[36] They also play a key role in efforts to exploit the computing abilities of a graphics processing unit (GPU).