Jump to navigation Jump to search


963 bytes added, 12:27, 2 November 2020
no edit summary
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 [https://[ workspace]], a collection of dfns website]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 [[wikipedia:lexical scoping|lexical scoping]], in contrast to the dynamic scoping of [[tradfn]]s. In other words, a dfn cannot "see" locals of its caller, but can see locals of its definer.
3 {⍵*÷⍺} 27 ⍝ ⍺th root
⍝ Multiline dfn with optional left parameterand conditional result
⍺←2 ⍝ square root by default
⍵*÷⍺ ⍝ result } ⍝ Dfn with conditional result root←{ ⍺←2 ⍝ square root by default ⍺=0:0 ⍝ return zero if zeroth root
⍵*÷⍺ ⍝ result
=== Error-guards ===
Dyalog APL dfns support error-guards for processing errors by error codes.<ref>[ Error Guards] – Dyalog APL.</ref>
In the following example, there are two error-guards for the error code 11 (DOMAIN ERROR):<ref>[ APL Error Messages and Codes] – Dyalog APL.</ref>
<source lang=apl>
G←6.6743E¯11 ⍝ gravitational constant
11::'N/A' ⍝ second DOMAIN ERROR: return 'N/A'
11::∇⍎¨⍵ ⍝ first DOMAIN ERROR: maybe the argument is a vector of strings?
G×⍵[1]×⍵[2]÷⍵[3]*2 ⍝ the argument is a vector of numbers
⍝ Calculate gravity force between the Earth and the Sun
Gravity '1.99e30' '5.97e24' '1.50e11'
Gravity 1.99e30 5.97e24 1.50e11
Gravity 1.99e30 5.97e24 0 ⍝ trigger division by zero
== External links ==
* [ Dyalog]
* [ GNU APL]
== References ==
{{APL syntax}}[[Category:Kinds of functions]][[Category:Defining functions]]

Navigation menu