120
edits
No edit summary |
|||
(16 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
A '''user-defined function''' (or '''tradfn''', 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. 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. | :''This page is about the function form typically written with the Del (<source lang=apl inline>∇</source>) 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. | |||
{{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 (<source lang=apl inline>∇</source>) 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 <source lang=apl inline>∇</source>s are not part of the definition. Indeed, if using the [[Fix Definition]] (<source lang=apl inline>⎕FX</source>) [[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]]. | |||
In many dialects the function header syntax of defined functions is adapted to allow user-defined [[operator]]s as well. A tradfn operator can also be called a '''tradop''' (pronounced "trad op"), parallel to how a "dfn" operator can be called a ''dop''. | In many dialects the function header syntax of defined functions is adapted to allow user-defined [[operator]]s as well. A tradfn operator can also be called a '''tradop''' (pronounced "trad op"), parallel to how a "dfn" operator can be called a ''dop''. | ||
Line 13: | Line 19: | ||
</source> | </source> | ||
{{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 === | |||
Assignments are global by default. To keep a name local, it must be mentioned in the header, separated from the rest of the header by a semi-colon: | |||
<source lang=apl> | |||
∇ r←l Tradfn r;intermediate | |||
intermediate←l r | |||
r←intermediate intermediate | |||
∇ | |||
</source> | |||
{{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]], [[APLX]], and every older APL from [[APL\360]]}} | |||
=== Braces === | === Braces === | ||
An [[ambivalent function]] with an optional left argument, a conditional [[control structure]], one local variable, and a [[#Shyness|shy]] result: | An [[ambivalent function]] with an optional left argument, a conditional [[control structure]], one local variable, and a [[#Shyness|shy]] result: | ||
Line 33: | Line 49: | ||
=== Brackets === | === Brackets === | ||
[[GNU APL]] allows functions and operators to accept an [[function axis|axis]] argument:<ref>[https://www.gnu.org/software/apl/apl.html#Section-3_002e2 | [[GNU APL]] allows functions and operators to accept an [[function axis|axis]] argument:<ref>[[GNU APL community]]. GNU APL Info Manual. [https://www.gnu.org/software/apl/apl.html#Section-3_002e2 Axis argument in defined functions].</ref> | ||
<source lang=apl> | <source lang=apl> | ||
∇ Z←Average[X] B | ∇ Z←Average[X] B | ||
Line 75: | Line 91: | ||
</source> | </source> | ||
{{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]], [[APLX]]}} | {{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]], [[APLX]]}} | ||
== Properties == | == Properties == | ||
Line 89: | Line 102: | ||
=== Dyalog APL extensions === | === Dyalog APL extensions === | ||
Dyalog APL's tradfns are enhanced in various ways compared to most other dialects.<ref>[[Dyalog Ltd.]] Programming Reference Guide. [https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/Model%20Syntax.htm Model Syntax].</ref> | Dyalog APL's tradfns are enhanced in various ways compared to most other dialects.<ref>[[Dyalog Ltd.]] Programming Reference Guide. [https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/TradFns/Model%20Syntax.htm Model Syntax].</ref> | ||
==== Dynamic localisation ==== | |||
Names can be localised dynamically, while a tradfn is running, using the [[Shadow Name]] (<source lang=apl inline>⎕SHADOW</source>) [[system function]].<ref>[[Dyalog Ltd.]] Language Reference Guide. [https://help.dyalog.com/latest/#Language/System%20Functions/shadow.htm Shadow Name].</ref> | Names can be localised dynamically, while a tradfn is running, using the [[Shadow Name]] (<source lang=apl inline>⎕SHADOW</source>) [[system function]].<ref>[[Dyalog Ltd.]] Language Reference Guide. [https://help.dyalog.com/latest/#Language/System%20Functions/shadow.htm Shadow Name].</ref> | ||
<source lang=apl> | <source lang=apl> | ||
Line 105: | Line 119: | ||
</source>{{Works in|[[Dyalog APL]]}} | </source>{{Works in|[[Dyalog APL]]}} | ||
Additional local names can also be declared on separate lines following the header line:<ref>[[Dyalog Ltd.]] Programming Reference Guide. [https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/Locals%20Lines.htm Locals Lines].</ref> | ==== Locals lines ==== | ||
Additional local names can also be declared on separate lines following the header line:<ref>[[Dyalog Ltd.]] Programming Reference Guide. [https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/TradFns/Locals%20Lines.htm Locals Lines].</ref> | |||
<source lang=apl> | <source lang=apl> | ||
name←1 2 3 | name←1 2 3 | ||
Line 119: | Line 134: | ||
</source>{{Works in|[[Dyalog APL]]}} | </source>{{Works in|[[Dyalog APL]]}} | ||
==== 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 <source lang=apl inline>result←{left} Function right</source>). 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. | The the header syntax can explicitly specify that a function is [[ambivalent]] by enclosing the left argument name in curly braces (for example <source lang=apl inline>result←{left} Function right</source>). 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 ==== | |||
A tradfn can return a function value as result. The returned function will replace the function and its arguments (if any) in the calling expression: | A tradfn can return a function value as result. The returned function will replace the function and its arguments (if any) in the calling expression: | ||
<source lang=apl> | <source lang=apl> | ||
Line 135: | Line 152: | ||
12 | 12 | ||
</source>{{Works in|[[Dyalog APL]]}} | </source>{{Works in|[[Dyalog APL]]}} | ||
The right argument and the result can be a name list instead of single name. The interpreter will unpack the right argument when the function is called, and collect the result when the function returns.<ref>[[Dyalog Ltd.]] Programming Reference Guide. [https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/Namelists.htm Namelists].</ref> | ==== Shyness ==== | ||
By default, functions in APL will print their result to the session log even without using <source lang=apl inline>⎕←</source> unless their results are shy. Shy results are declared by putting curly braces around the result name (for example <source lang=apl inline>{result}←left Function right</source>). Assignment (<source lang=apl inline>name←array</source>) 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 ==== | |||
The right argument and the result can be a name list instead of single name. The interpreter will unpack the right argument when the function is called, and collect the result when the function returns.<ref>[[Dyalog Ltd.]] Programming Reference Guide. [https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/TradFns/Namelists.htm Namelists].</ref> | |||
<source lang=apl> | <source lang=apl> | ||
∇ (c b a)←Rev(a b c) | ∇ (c b a)←Rev(a b c) | ||
Line 145: | Line 164: | ||
3 2 1 | 3 2 1 | ||
</source>{{Works in|[[Dyalog APL]]}} | </source>{{Works in|[[Dyalog APL]]}} | ||
== A+ == | |||
=== A+ differences=== | |||
[[A+]] uses a reworked style of function and operator definition than maintains the principle of a header that matches the way the function will be used, but differs in many details: | [[A+]] uses a reworked style of function and operator definition than maintains the principle of a header that matches the way the function will be used, but differs in many details: | ||
* The result name is not included in the header; instead, the result of the last executed statement is returned (and so functions that do not return a result cannot be defined). | * The result name is not included in the header; instead, the result of the last executed statement is returned (and so functions that do not return a result cannot be defined). | ||
* The header is separated from the body with a colon, and the body of a multi-line function is enclosed in curly braces. | * The header is separated from the body with a colon, and the body of a multi-line function is enclosed in curly braces. | ||
* Functions have lexical scope. Variables assigned are local by default, and can be made global by enclosing their names in parentheses when assigning. | * Functions have lexical scope. Variables assigned are local by default, and can be made global by enclosing their names in parentheses when assigning. | ||
==External links== | ==External links== | ||
Line 160: | Line 177: | ||
=== Documentation === | === Documentation === | ||
* [https://help.dyalog.com/latest/#Language/Introduction/Functions.htm | * Dyalog: [https://help.dyalog.com/latest/#Language/Introduction/Functions.htm Functions], [https://help.dyalog.com/latest/#Language/Introduction/Operators.htm Operators] | ||
* APLX: [http://microapl.com/apl_help/ch_020_010_070.htm User-defined Functions], [http://microapl.com/apl_help/ch_020_010_090.htm User-defined Operators] | |||
* [http://microapl.com/apl_help/ch_020_010_070.htm | |||
=== References === | === References === |