A direct function (dfn, pronounced "dee fun") is an alternative way to define a function and operator (a
higher-order function
In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:
* takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itself ...
) in the programming language
APL. A direct operator can also be called a dop (pronounced "dee op"). They were invented by
John Scholes John Scholes may refer to:
* John M. Scholes (1948–2019), computer scientist and APL implementer and programmer
*John Scholes (cricketer)
Walter John Scholes (5 January 1950 – 14 July 2003) was an Australian first-class cricketer and coach. ...
in 1996.
They are a unique combination of
array programming
In computer science, array programming refers to solutions which allow the application of operations to an entire set of values at once. Such solutions are commonly used in scientific and engineering settings.
Modern programming languages that s ...
, higher-order function, and
functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declar ...
, 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 , separated by or new-lines, wherein denotes the left argument and the right, and denotes
recursion
Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics ...
(function self-reference). For example, the function tests whether each row of is a
Pythagorean triplet
A Pythagorean triple consists of three positive integers , , and , such that . Such a triple is commonly written , and a well-known example is . If is a Pythagorean triple, then so is for any positive integer . A primitive Pythagorean triple is ...
(by testing whether the sum of squares equals twice the square of the maximum).
PT←
PT 3 4 5
1
x
4 5 3
3 11 6
5 13 12
17 16 8
11 12 4
17 15 8
PT x
1 0 1 0 0 1
The
factorial
In mathematics, the factorial of a non-negative denoted is the product of all positive integers less than or equal The factorial also equals the product of n with the next smaller factorial:
\begin
n! &= n \times (n-1) \times (n-2) \t ...
function as a dfn:
fact←
fact 5
120
fact¨ ⍳10 ⍝ fact applied to each element of 0 to 9
1 1 2 6 24 120 720 5040 40320 362880
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 , separated by or new-lines.
expression
guard: expression
guard:
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
Assignment, assign or The Assignment may refer to:
* Homework
* Sex assignment
* The process of sending National Basketball Association players to its development league; see
Computing
* Assignment (computer science), a type of modification to ...
, 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
Local may refer to:
Geography and transportation
* Local (train), a train serving local traffic demand
* Local, Missouri, a community in the United States
* Local government, a form of public administration, usually the lowest tier of administrat ...
by default, with
lexical scope
In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts o ...
.
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 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 is not evaluated otherwise.
denotes
recursion
Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics ...
or self-reference by the function, and denotes self-reference by the operator. Such denotation permits
anonymous recursion
In computer science, anonymous recursion is recursion which does not explicitly call a function by name. This can be done either explicitly, by using a higher-order function – passing in a function as an argument and calling it – or implicitly ...
.
Error trapping is provided through error-guards, . 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 adds to ( or ) times .
3 4
3J4
∘.⍨ ¯2+⍳5
¯2J¯2 ¯2J¯1 ¯2 ¯2J1 ¯2J2
¯1J¯2 ¯1J¯1 ¯1 ¯1J1 ¯1J2
0J¯2 0J¯1 0 0J1 0J2
1J¯2 1J¯1 1 1J1 1J2
2J¯2 2J¯1 2 2J1 2J2
The significance of this function can be seen as follows:
Moreover, analogous to that monadic ⇔ (''negate'') and monadic ⇔ (''reciprocal''), a monadic definition of the function is useful, effected by specifying a default value of 0 for : if , then ⇔ ⇔ .
j←
3 j 4 ¯5.6 7.89
3J4 3J¯5.6 3J7.89
j 4 ¯5.6 7.89
0J4 0J¯5.6 0J7.89
sin← 1∘○
cos← 2∘○
Euler←
Euler (¯0.5+?10⍴0) j (¯0.5+?10⍴0)
1 1 1 1 1 1 1 1 1 1
The last expression illustrates Euler's formula
Euler's formula, named after Leonhard Euler, is a mathematical formula in complex analysis that establishes the fundamental relationship between the trigonometric functions and the complex exponential function. Euler's formula states that for an ...
on ten random numbers with real and imaginary parts in the interval .
Single recursion
The ternary construction of the Cantor set
In mathematics, the Cantor set is a set of points lying on a single line segment that has a number of unintuitive properties. It was discovered in 1874 by Henry John Stephen Smith and introduced by German mathematician Georg Cantor in 1883.
Thr ...
starts with the interval ,1and at each stage removes the middle third from each remaining subinterval:
The Cantor set of order defined as a dfn:
Cantor←
Cantor 0
1
Cantor 1
1 0 1
Cantor 2
1 0 1 0 0 0 1 0 1
Cantor 3
1 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 1
Cantor 0 to Cantor 6 depicted as black bars:
The function computes a bit vector of length so that bit (for and ) is 1 if and only if is a prime
A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways ...
.[
sieve←
10 10 ⍴ sieve 100
0 0 1 1 0 1 0 1 0 0
0 1 0 1 0 0 0 1 0 1
0 0 0 1 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0 0
0 1 0 1 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0 0
0 1 0 1 0 0 0 0 0 1
0 0 0 1 0 0 0 0 0 1
0 0 0 0 0 0 0 1 0 0
b←sieve 1e9
≢b
1000000000
(10*⍳10) (+⌿↑)⍤0 1 ⊢b
0 4 25 168 1229 9592 78498 664579 5761455 50847534
The last sequence, the number of primes less than powers of 10, is an initial segment of . The last number, 50847534, is the number of primes less than . It is called Bertelsen's number, memorably described by ]MathWorld
''MathWorld'' is an online mathematics reference work, created and largely written by Eric W. Weisstein. It is sponsored by and licensed to Wolfram Research, Inc. and was partially funded by the National Science Foundation's National Science Dig ...
as "an erroneous name erroneously given the erroneous value of ".
uses two different methods to mark composites with 0s, both effected using local anonymous dfns: The first uses the sieve of Eratosthenes
In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit.
It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime n ...
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 .) The second finds the smallest new prime remaining in (), and sets to 0 bit itself and bits at times the numbers at remaining 1 bits in an initial segment of (). This second dfn uses tail recursion.
Tail recursion
Typically, the factorial
In mathematics, the factorial of a non-negative denoted is the product of all positive integers less than or equal The factorial also equals the product of n with the next smaller factorial:
\begin
n! &= n \times (n-1) \times (n-2) \t ...
function is define recursively (as above), but it can be coded to exploit tail recursion
In computer science, a tail call is a subroutine call performed as the final action of a procedure. If the target of a tail is the same subroutine, the subroutine is said to be tail recursive, which is a special case of direct recursion. Tail recur ...
by using an accumulator left argument:
fac←
Similarly, the determinant
In mathematics, the determinant is a scalar value that is a function of the entries of a square matrix. It characterizes some properties of the matrix and the linear map represented by the matrix. In particular, the determinant is nonzero if and ...
of a square complex matrix using Gaussian elimination
In mathematics, Gaussian elimination, also known as row reduction, is an algorithm for solving systems of linear equations. It consists of a sequence of operations performed on the corresponding matrix of coefficients. This method can also be used ...
can be computed with tail recursion:
det←
Multiple recursion
A partition
Partition may refer to:
Computing Hardware
* Disk partitioning, the division of a hard disk drive
* Memory partition, a subdivision of a computer's memory, usually for use by a single job
Software
* Partition (database), the division of a ...
of a non-negative integer is a vector of positive integers such that , where the order in is not significant. For example, and are partitions of 4, and and and are considered to be the same partition.
The partition function counts the number of partitions. The function is of interest in number theory
Number theory (or arithmetic or higher arithmetic in older usage) is a branch of pure mathematics devoted primarily to the study of the integers and arithmetic function, integer-valued functions. German mathematician Carl Friedrich Gauss (1777� ...
, studied by Euler
Leonhard Euler ( , ; 15 April 170718 September 1783) was a Swiss mathematician, physicist, astronomer, geographer, logician and engineer who founded the studies of graph theory and topology and made pioneering and influential discoveries in ma ...
, Hardy
Hardy may refer to:
People
* Hardy (surname)
* Hardy (given name)
* Hardy (singer), American singer-songwriter Places Antarctica
* Mount Hardy, Enderby Land
* Hardy Cove, Greenwich Island
* Hardy Rocks, Biscoe Islands
Australia
* Hardy, Sout ...
, Ramanujan, Erdős
Erdős, Erdos, or Erdoes is a Hungarian surname.
People with the surname include:
* Ágnes Erdős (born 1950), Hungarian politician
* Brad Erdos (born 1990), Canadian football player
* Éva Erdős (born 1964), Hungarian handball player
* Józse ...
, and others. The recurrence relation
: