Dfn: Difference between revisions

Jump to navigation Jump to search
1,443 bytes added ,  22:11, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
No edit summary
m (Text replacement - "<source" to "<syntaxhighlight")
 
(5 intermediate revisions by 3 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").


Line 8: Line 10:


== Examples ==
== Examples ==
<source lang=apl>
<syntaxhighlight lang=apl>
       {⍵*0.5} 16        ⍝ square root
       {⍵*0.5} 16        ⍝ square root
4
4
       3 {⍵*÷⍺} 27      ⍝ ⍺th root
       3 {⍵*÷⍺} 27      ⍝ ⍺th root
3
3</syntaxhighlight>
      ⍝ Multiline dfn with optional left parameter and conditional result
=== Default left arguments ===
Assignment to <syntaxhighlight lang=apl inline>⍺</syntaxhighlight> is unusual in that the entire statement is only executed if the dfn is called monadically:<ref>[https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/DynamicFunctions/Default%20Left%20Argument.htm Default Left Argument] – Dyalog APL.</ref>
<syntaxhighlight lang=apl>
       root←{
       root←{
           ⍺←2          ⍝ square root by default
           ⍺←2          ⍝ square root by default
          ⍵*÷⍺          ⍝ result
      }</syntaxhighlight>
=== Guards ===
Guards provide dfns with support for basic flow control.<ref>[https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/DynamicFunctions/Guards.htm Guards] – Dyalog APL.</ref> This is a multiline dfn with a conditional result:
<syntaxhighlight lang=apl>
      root←{
           ⍺=0:0        ⍝ return zero if zeroth root
           ⍺=0:0        ⍝ return zero if zeroth root
           ⍵*÷⍺          ⍝ result
           ⍵*÷⍺          ⍝ result
       }</source>
       }</syntaxhighlight>


=== Error-guards ===
=== Error-guards ===
Line 24: Line 34:


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>
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>
<syntaxhighlight lang=apl>
Gravity←{
Gravity←{
     G←6.6743E¯11      ⍝ gravitational constant
     G←6.6743E¯11      ⍝ gravitational constant
Line 39: Line 49:
       Gravity 1.99e30 5.97e24 0  ⍝ trigger division by zero
       Gravity 1.99e30 5.97e24 0  ⍝ trigger division by zero
N/A
N/A
</source>
</syntaxhighlight>
=== Shy results ===
[[Roger Hui]]'s <syntaxhighlight lang=apl inline>assert</syntaxhighlight> is a dfn that has become the de facto standard when it comes to test suites.<ref>Stefan Kruger. [https://www.dyalog.com/blog/2021/04/2020-problem-solving-competition-phase-ii-highlights/ 2020 Problem Solving Competition – Phase II highlights]. [[Dyalog Ltd.|Dyalog]] blog. April 30, 2021.</ref>. In it, Hui uses both a [[default left argument]] and a final assignment to make the dfn [[shy]]:
<syntaxhighlight lang=apl>
assert ← {⍺←'assertion failure' ⋄ 0∊⍵:⍺ ⎕signal 8 ⋄ shy←0}</syntaxhighlight>


== External links ==
== External links ==

Navigation menu