Dfn: Difference between revisions
Jump to navigation
Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>") |
m (Text replacement - "<source" to "<syntaxhighlight") |
||
Line 10: | Line 10: | ||
== Examples == | == Examples == | ||
< | <syntaxhighlight lang=apl> | ||
{⍵*0.5} 16 ⍝ square root | {⍵*0.5} 16 ⍝ square root | ||
4 | 4 | ||
Line 16: | Line 16: | ||
3</syntaxhighlight> | 3</syntaxhighlight> | ||
=== Default left arguments === | === Default left arguments === | ||
Assignment to < | 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 | ||
Line 24: | Line 24: | ||
=== Guards === | === 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: | 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←{ | root←{ | ||
⍺=0:0 ⍝ return zero if zeroth root | ⍺=0:0 ⍝ return zero if zeroth root | ||
Line 34: | 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> | ||
< | <syntaxhighlight lang=apl> | ||
Gravity←{ | Gravity←{ | ||
G←6.6743E¯11 ⍝ gravitational constant | G←6.6743E¯11 ⍝ gravitational constant | ||
Line 51: | Line 51: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Shy results === | === Shy results === | ||
[[Roger Hui]]'s < | [[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> | assert ← {⍺←'assertion failure' ⋄ 0∊⍵:⍺ ⎕signal 8 ⋄ shy←0}</syntaxhighlight> | ||