Dfn

From APL Wiki
Revision as of 11:54, 23 August 2020 by Andrii Makukha (talk | contribs)
Jump to navigation Jump to search

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.

Contents

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
      }

External links