# Difference between revisions of "Dfn"

(Disambiguate from dfns workspace) |
|||

(10 intermediate revisions by 4 users not shown) | |||

Line 1: | Line 1: | ||

+ | : ''"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"). | 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 [ | + | 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 [[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. | 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. | ||

Wikipedia includes a thorough treatment of [[Wikipedia:dfns|dfns]]. | Wikipedia includes a thorough treatment of [[Wikipedia:dfns|dfns]]. | ||

+ | |||

+ | == Examples == | ||

+ | <source lang=apl> | ||

+ | {⍵*0.5} 16 ⍝ square root | ||

+ | 4 | ||

+ | 3 {⍵*÷⍺} 27 ⍝ ⍺th root | ||

+ | 3 | ||

+ | ⍝ Multiline dfn with optional left parameter and conditional result | ||

+ | root←{ | ||

+ | ⍺←2 ⍝ square root by default | ||

+ | ⍺=0:0 ⍝ return zero if zeroth root | ||

+ | ⍵*÷⍺ ⍝ result | ||

+ | }</source> | ||

+ | |||

+ | === Error-guards === | ||

+ | Dyalog APL dfns support error-guards for processing errors by error codes.<ref>[https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/DynamicFunctions/Error%20Guards.htm Error Guards] – Dyalog APL.</ref> | ||

+ | |||

+ | In the following example, there are two error-guards for the error code 11 (DOMAIN ERROR):<ref>[https://help.dyalog.com/latest/#Language/Errors/APL%20Errors.htm#APLErrors APL Error Messages and Codes] – Dyalog APL.</ref> | ||

+ | <source lang=apl> | ||

+ | Gravity←{ | ||

+ | 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' | ||

+ | 3.524119391E22 | ||

+ | Gravity 1.99e30 5.97e24 1.50e11 | ||

+ | 3.524119391E22 | ||

+ | Gravity 1.99e30 5.97e24 0 ⍝ trigger division by zero | ||

+ | N/A | ||

+ | </source> | ||

+ | |||

== External links == | == External links == | ||

+ | * [[wikipedia:Direct function|Direct function]] | ||

+ | |||

=== Tutorials === | === Tutorials === | ||

* APL Cultivation: [https://chat.stackexchange.com/rooms/52405/conversation/lesson-2-diving-into-functions-in-apl Diving Into Functions in APL] | * APL Cultivation: [https://chat.stackexchange.com/rooms/52405/conversation/lesson-2-diving-into-functions-in-apl Diving Into Functions in APL] | ||

+ | * Mastering Dyalog: [https://www.dyalog.com/uploads/documents/MasteringDyalogAPL.pdf#page=174 page 174] | ||

+ | |||

=== Documentation === | === Documentation === | ||

− | * [ | + | * [https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/DynamicFunctions/Dynamic%20Functions%20and%20Operators.htm Dyalog] |

+ | * [https://www.gnu.org/software/apl/apl.html#Section-3_002e7 GNU APL] | ||

+ | |||

+ | == References == | ||

+ | <references/> | ||

+ | |||

+ | {{APL syntax}}[[Category:Kinds of functions]][[Category:Defining functions]] |

## Latest revision as of 16:00, 5 February 2021

*"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.

## Examples

```
{⍵*0.5} 16 ⍝ square root
4
3 {⍵*÷⍺} 27 ⍝ ⍺th root
3
⍝ Multiline dfn with optional left parameter and 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.^{[1]}

In the following example, there are two error-guards for the error code 11 (DOMAIN ERROR):^{[2]}

```
Gravity←{
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'
3.524119391E22
Gravity 1.99e30 5.97e24 1.50e11
3.524119391E22
Gravity 1.99e30 5.97e24 0 ⍝ trigger division by zero
N/A
```

## External links

### Tutorials

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

### Documentation

## References

- ↑ Error Guards – Dyalog APL.
- ↑ APL Error Messages and Codes – Dyalog APL.