4,494
edits
(Disambiguate from dfns workspace) |
m (Text replacement - "<source" to "<syntaxhighlight") |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 10: | Line 10: | ||
== Examples == | == Examples == | ||
< | <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> | ||
=== 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 | ||
}</ | }</syntaxhighlight> | ||
=== Error-guards === | === Error-guards === | ||
Line 26: | 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 41: | 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 | ||
</ | </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 == |