Dfn


 * "dfns" redirects here. For the workspace by Scholes, see dfns workspace.

A dfn (contraction of direct function or dynamic function, pronounced "dee fun") is an alternative way to define a function and operator, invented by John Scholes. A dfn operator can also be called a dop (pronounced "dee op").

John Scholes was responsible for numerous presentations and publications on and about dfns, and until his passing he maintained the dfns workspace, a collection of dfns examples.

As of 2020, dfns are fully implemented in Dyalog APL, NARS2000, ngn/apl, dzaima/APL, and partially in GNU APL, although not all dialects implement lexical scoping, in contrast to the dynamic scoping of tradfns. In other words, a dfn cannot "see" locals of its caller, but can see locals of its definer.

Wikipedia includes a thorough treatment of dfns.

Default left arguments
Assignment to  is unusual in that the entire statement is only executed if the dfn is called monadically:

Guards
Guards provide dfns with support for basic flow control. This is a multiline dfn with a conditional result:

Error-guards
Dyalog APL dfns support error-guards for processing errors by error codes.

In the following example, there are two error-guards for the error code 11 (DOMAIN ERROR):

Shy results
Roger Hui's  is a dfn that has become the de facto standard when it comes to test suites. . In it, Hui uses both a default left argument and a final assignment to make the dfn shy:

Tutorials

 * APL Cultivation: Diving Into Functions in APL
 * Mastering Dyalog: page 174

Documentation

 * Dyalog
 * GNU APL