Niladic function: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Marshall moved page Niladic to Niladic function)
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
In [[APL syntax]], a '''niladic function''' is a special type of [[function]] that takes no [[argument]]s and is evaluated immediately when it is encountered in a statement. It cannot be used as an [[operand]] because this evaluation happens before operator binding, or any other evaluation step such as [[stranding]]. For this reason operators are always defined to take non-niladic operands. Niladic is considered one of three possible [[function valence]]s along with [[monadic]] and [[dyadic]].
In [[APL syntax]], a '''niladic function''' is a special type of [[function]] that takes no [[argument]]s and is evaluated immediately when it is encountered in a statement. It cannot be used as an [[operand]] because this evaluation happens before operator binding, or any other evaluation step such as [[stranding]]. For this reason operators are always defined to take non-niladic operands. Niladic is considered one of three possible [[function valence]]s along with [[monadic]] and [[dyadic]].


[[System variable]]s like <source lang=apl inline>⎕TS</source> might be considered instances of niladic functions but are usually not described this way.
[[System variable]]s like <syntaxhighlight lang=apl inline>⎕TS</syntaxhighlight> ([[time stamp]]) might be considered instances of niladic functions but are usually not described this way.


A niladic function cannot be created as a [[derived function]] or [[dfn]]. However, an APL programmer can define a [[tradfn]] which takes no arguments:
In most dialects, a niladic function cannot be created as a [[derived function]] or [[dfn]]. However, an APL programmer can define a [[tradfn]] which takes no arguments:
<source lang=apl>
<syntaxhighlight lang=apl>
∇ res←set_a
∇ res←set_a
a ← 'a'
a ← 'a'
res ← 1
res ← 1
</source>
</syntaxhighlight>
After defining such a function, it can be evaluated simply by writing its name, with no need to indicate any arguments. A niladic function is the only possible named entity with this behaviour: for any other kind of value, writing the name alone in a statement will simply return that value, but for a niladic function it evaluates the function and returns the result.
After defining such a function, it can be evaluated simply by writing its name, with no need to indicate any arguments. A niladic function is the only possible named entity with this behaviour: for any other kind of value, writing the name alone in a statement will simply return that value, but for a niladic function it evaluates the function and returns the result.
<source lang=apl>
<syntaxhighlight lang=apl>
       a
       a
VALUE ERROR: Undefined name: a
VALUE ERROR: Undefined name: a
Line 20: Line 20:
       a
       a
a
a
</source>
</syntaxhighlight>


{{APL syntax}}
{{APL syntax}}[[Category:Kinds of functions]][[Category:Syntactic elements]]

Latest revision as of 22:30, 10 September 2022

In APL syntax, a niladic function is a special type of function that takes no arguments and is evaluated immediately when it is encountered in a statement. It cannot be used as an operand because this evaluation happens before operator binding, or any other evaluation step such as stranding. For this reason operators are always defined to take non-niladic operands. Niladic is considered one of three possible function valences along with monadic and dyadic.

System variables like ⎕TS (time stamp) might be considered instances of niladic functions but are usually not described this way.

In most dialects, a niladic function cannot be created as a derived function or dfn. However, an APL programmer can define a tradfn which takes no arguments:

∇ res←set_a
a ← 'a'
res ← 1
∇

After defining such a function, it can be evaluated simply by writing its name, with no need to indicate any arguments. A niladic function is the only possible named entity with this behaviour: for any other kind of value, writing the name alone in a statement will simply return that value, but for a niladic function it evaluates the function and returns the result.

      a
VALUE ERROR: Undefined name: a
      a
      ∧
      set_a
1
      a
a


APL syntax [edit]
General Comparison with traditional mathematicsPrecedenceTacit programming (Train, Hook, Split composition)
Array Numeric literalStringStrand notationObject literalArray notation (design considerations)
Function ArgumentFunction valenceDerived functionDerived operatorNiladic functionMonadic functionDyadic functionAmbivalent functionDefined function (traditional)DfnFunction train
Operator OperandOperator valenceTradopDopDerived operator
Assignment MultipleIndexedSelectiveModified
Other Function axisBracket indexingBranchStatement separatorQuad nameSystem commandUser commandKeywordDot notationFunction-operator overloadingControl structureComment