Defined function (traditional): Difference between revisions

Jump to navigation Jump to search
subheadings
No edit summary
(subheadings)
Line 92: Line 92:
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/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 106: Line 107:
</source>{{Works in|[[Dyalog APL]]}}
</source>{{Works in|[[Dyalog APL]]}}


==== 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/Locals%20Lines.htm Locals Lines].</ref>
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>
<source lang=apl>
<source lang=apl>
Line 120: Line 122:
</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 136: Line 140:
12
12
</source>{{Works in|[[Dyalog APL]]}}
</source>{{Works in|[[Dyalog APL]]}}
{{Anchor|Shyness}}
 
==== Shyness ====
A tradfn can be declared as shy, that is, it exhibits the same behaviour as an assignment, in that by default, no result is printed when the function terminates, but attempting to use the result still succeeds. This declaration is done by putting curly braces around the result name (for example <source lang=apl inline>{result}←left Function right</source>).
A tradfn can be declared as shy, that is, it exhibits the same behaviour as an assignment, in that by default, no result is printed when the function terminates, but attempting to use the result still succeeds. This declaration is done by putting curly braces around the result name (for example <source lang=apl inline>{result}←left Function right</source>).


==== 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/Namelists.htm Namelists].</ref>
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>
<source lang=apl>
<source lang=apl>
Line 146: Line 152:
3 2 1
3 2 1
</source>{{Works in|[[Dyalog APL]]}}
</source>{{Works in|[[Dyalog APL]]}}
=== A+ differences===
=== 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:

Navigation menu