Dfn: Difference between revisions

Jump to navigation Jump to search
656 bytes added ,  10:33, 7 June 2021
(Disambiguate from dfns workspace)
Line 14: Line 14:
4
4
       3 {⍵*÷⍺} 27      ⍝ ⍺th root
       3 {⍵*÷⍺} 27      ⍝ ⍺th root
3
3</source>
      ⍝ Multiline dfn with optional left parameter and conditional result
=== Default left arguments ===
Assignment to <source lang=apl inline>⍺</source> 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>
<source lang=apl>
       root←{
       root←{
           ⍺←2          ⍝ square root by default
           ⍺←2          ⍝ square root by default
          ⍵*÷⍺          ⍝ result
      }</source>
=== 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:
<source lang=apl>
      root←{
           ⍺=0:0        ⍝ return zero if zeroth root
           ⍺=0:0        ⍝ return zero if zeroth root
           ⍵*÷⍺          ⍝ result
           ⍵*÷⍺          ⍝ result

Navigation menu