4,493
edits
m (Text replacement - "<source" to "<syntaxhighlight") |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
Line 1: | Line 1: | ||
:''This page is about the function form typically written with the Del (<syntaxhighlight lang=apl inline>∇</ | :''This page is about the function form typically written with the Del (<syntaxhighlight lang=apl inline>∇</syntaxhighlight>) character. See [[Function styles]] for an overview of all forms; in particular [[dfn]]s are considered a type of defined function in [[Dyalog APL]] and [[GNU APL]].'' | ||
A '''user-defined function''' (or '''tradfn''', pronounced "trad fun", for "traditional function", in [[Dyalog APL]]) is a function defined using a header that includes the function's name. Introduced in [[APL\360]], function definition was universally supported by APL dialects for much of the language's [[history]], and is still commonly used in mainstream APLs. Since the 1990s other ways to [[Function styles|describe functions]] have appeared, with [[J]] and [[K]] rejecting function definition in favor of [[anonymous function]] description. | A '''user-defined function''' (or '''tradfn''', pronounced "trad fun", for "traditional function", in [[Dyalog APL]]) is a function defined using a header that includes the function's name. Introduced in [[APL\360]], function definition was universally supported by APL dialects for much of the language's [[history]], and is still commonly used in mainstream APLs. Since the 1990s other ways to [[Function styles|describe functions]] have appeared, with [[J]] and [[K]] rejecting function definition in favor of [[anonymous function]] description. | ||
{{Anchor|Representations}}The canonical representation form is equivalent to what the user would type into the [[line editor]] to define the function.<ref>[[Dyalog Ltd.]] Programming Reference Guide. [https://help.dyalog.com/latest/#Language/System%20Functions/cr.htm Canonical Representation].</ref> An alternative representation consists of the entire [[session]] log transcript, including [[Del]]s (<syntaxhighlight lang=apl inline>∇</ | {{Anchor|Representations}}The canonical representation form is equivalent to what the user would type into the [[line editor]] to define the function.<ref>[[Dyalog Ltd.]] Programming Reference Guide. [https://help.dyalog.com/latest/#Language/System%20Functions/cr.htm Canonical Representation].</ref> An alternative representation consists of the entire [[session]] log transcript, including [[Del]]s (<syntaxhighlight lang=apl inline>∇</syntaxhighlight>) and line numbers, after having such a definition has been made.<ref>[[Dyalog Ltd.]] Language Reference Guide. [https://help.dyalog.com/latest/#Language/System%20Functions/vr.htm Vector Representation].</ref> However, it should be noted that the <syntaxhighlight lang=apl inline>∇</syntaxhighlight>s are not part of the definition. Indeed, if using the [[Fix Definition]] (<syntaxhighlight lang=apl inline>⎕FX</syntaxhighlight>) [[system function]] to define the function, Dels need not (or must not, depending on implementation) be included. | ||
Beginning in the 2010s [[Dyalog]]-based APL dialects including [[ngn/apl]], [[dzaima/APL]], and [[APL\iv]] have removed function definition in favor of [[dfn]]s. Wikipedia has a comparison of [[Wikipedia:Direct_function#Dfns_versus_tradfns|dfns versus tradfns]]. | Beginning in the 2010s [[Dyalog]]-based APL dialects including [[ngn/apl]], [[dzaima/APL]], and [[APL\iv]] have removed function definition in favor of [[dfn]]s. Wikipedia has a comparison of [[Wikipedia:Direct_function#Dfns_versus_tradfns|dfns versus tradfns]]. | ||
Line 17: | Line 17: | ||
r←l r | r←l r | ||
∇ | ∇ | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]], [[APLX]], and every older APL from [[APL\360]]}} | {{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]], [[APLX]], and every older APL from [[APL\360]]}} | ||
=== Semi-colons === | === Semi-colons === | ||
Line 26: | Line 26: | ||
r←intermediate intermediate | r←intermediate intermediate | ||
∇ | ∇ | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]], [[APLX]], and every older APL from [[APL\360]]}} | {{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]], [[APLX]], and every older APL from [[APL\360]]}} | ||
Line 45: | Line 45: | ||
10×1 AddMult2 3 ⍝ use result anyway | 10×1 AddMult2 3 ⍝ use result anyway | ||
80 | 80 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]]}} | {{Works in|[[Dyalog APL]]}} | ||
Line 58: | Line 58: | ||
Average[2] 5 5⍴⍳25 | Average[2] 5 5⍴⍳25 | ||
3 8 13 18 23 | 3 8 13 18 23 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[GNU APL]]}} | {{Works in|[[GNU APL]]}} | ||
=== Operators === | === Operators === | ||
Line 74: | Line 74: | ||
÷HOOK|2 0 ¯7 | ÷HOOK|2 0 ¯7 | ||
1 1 ¯1 | 1 1 ¯1 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]], [[APLX]]}} | {{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]], [[APLX]]}} | ||
A monadic operator and a dyadic operator, both deriving [[dyadic function]]s: | A monadic operator and a dyadic operator, both deriving [[dyadic function]]s: | ||
Line 89: | Line 89: | ||
2 ¯7 2+OVER|¯3 1 4 | 2 ¯7 2+OVER|¯3 1 4 | ||
5 8 6 | 5 8 6 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]], [[APLX]]}} | {{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]], [[APLX]]}} | ||
Line 105: | Line 105: | ||
==== Dynamic localisation ==== | ==== Dynamic localisation ==== | ||
Names can be localised dynamically, while a tradfn is running, using the [[Shadow Name]] (<syntaxhighlight lang=apl inline>⎕SHADOW</ | Names can be localised dynamically, while a tradfn is running, using the [[Shadow Name]] (<syntaxhighlight lang=apl inline>⎕SHADOW</syntaxhighlight>) [[system function]].<ref>[[Dyalog Ltd.]] Language Reference Guide. [https://help.dyalog.com/latest/#Language/System%20Functions/shadow.htm Shadow Name].</ref> | ||
<syntaxhighlight lang=apl> | <syntaxhighlight lang=apl> | ||
name←1 2 3 | name←1 2 3 | ||
Line 117: | Line 117: | ||
name | name | ||
1 2 3 | 1 2 3 | ||
</ | </syntaxhighlight>{{Works in|[[Dyalog APL]]}} | ||
==== Locals lines ==== | ==== Locals lines ==== | ||
Line 132: | Line 132: | ||
name | name | ||
1 2 3 | 1 2 3 | ||
</ | </syntaxhighlight>{{Works in|[[Dyalog APL]]}} | ||
==== Explicit ambivalence ==== | ==== Explicit ambivalence ==== | ||
The the header syntax can explicitly specify that a function is [[ambivalent]] by enclosing the left argument name in curly braces (for example <syntaxhighlight lang=apl inline>result←{left} Function right</ | The the header syntax can explicitly specify that a function is [[ambivalent]] by enclosing the left argument name in curly braces (for example <syntaxhighlight lang=apl inline>result←{left} Function right</syntaxhighlight>). Others dialects treat all functions that declare a left argument name as ambivalent and leave it up to the programmer to check for the presence of a value. | ||
==== Function results ==== | ==== Function results ==== | ||
Line 151: | Line 151: | ||
3(Apply'times')4 | 3(Apply'times')4 | ||
12 | 12 | ||
</ | </syntaxhighlight>{{Works in|[[Dyalog APL]]}} | ||
==== Shyness ==== | ==== Shyness ==== | ||
By default, functions in APL will print their result to the session log even without using <syntaxhighlight lang=apl inline>⎕←</ | By default, functions in APL will print their result to the session log even without using <syntaxhighlight lang=apl inline>⎕←</syntaxhighlight> unless their results are shy. Shy results are declared by putting curly braces around the result name (for example <syntaxhighlight lang=apl inline>{result}←left Function right</syntaxhighlight>). Assignment (<syntaxhighlight lang=apl inline>name←array</syntaxhighlight>) exhibits the same behaviour as a shy function in that, by default, no result is printed when the function terminates, but attempting to use the result still succeeds. | ||
==== Namelists ==== | ==== Namelists ==== | ||
Line 163: | Line 163: | ||
Rev 1 2 3 | Rev 1 2 3 | ||
3 2 1 | 3 2 1 | ||
</ | </syntaxhighlight>{{Works in|[[Dyalog APL]]}} | ||
=== A+ differences=== | === A+ differences=== |