Monadic operator: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(Derived monadic operator)
m (Text replacement - "<source" to "<syntaxhighlight")
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
In [[APL syntax]], a '''monadic operator''' (or '''adverb''') is an [[operator]] with one [[operand]], which is written to the operator's left. [[Outer Product]] is usually also considered a monadic operator, even though it is written with two symbols and the operand on the right. A [[specified axis]] may also be considered a monadic operator, even though its syntax is anomalous: it is written with brackets, and can apply to the [[Reduce]] and [[Scan]] operators even though operators are not usually allowed to be operands.
In [[APL syntax]], a '''monadic operator''' (or '''adverb''') is an [[operator]] with one [[operand]], which is written to the operator's left. [[Outer Product]] is usually also considered a monadic operator, even though it is written with two symbols and the operand on the right. A [[specified axis]] may also be considered a monadic operator, even though its syntax is anomalous: it is written with brackets, and can apply to the [[Reduce]] and [[Scan]] operators even though operators are not usually allowed to be operands.


The term "monadic operator" refers to the [[operator valence|valence]] of the operator itself, that is, the number of operands. When applied, it produces a [[derived function]], which can have a different [[function valence]]. For example, the [[Windowed Reduce|2-wise reduction]] <source lang=apl inline>2 =/ 1 2 2 3</source> illustrates the monadic operator [[Reduce]] (<source lang=apl inline>/</source>) applied to a single operand <source lang=apl inline>=</source>, and then to two arguments <source lang=apl inline>2</source> and <source lang=apl inline>1 2 2 3</source>. We say it is a "monadic operator called dyadically".
The term "monadic operator" refers to the [[operator valence|valence]] of the operator itself, that is, the number of operands. When applied, it produces a [[derived function]], which can have a different [[function valence]]. For example, the [[Windowed Reduce|2-wise reduction]] <syntaxhighlight lang=apl inline>2 =/ 1 2 2 3</syntaxhighlight> illustrates the monadic operator [[Reduce]] (<syntaxhighlight lang=apl inline>/</syntaxhighlight>) applied to a single operand <syntaxhighlight lang=apl inline>=</syntaxhighlight>, and then to two arguments <syntaxhighlight lang=apl inline>2</syntaxhighlight> and <syntaxhighlight lang=apl inline>1 2 2 3</syntaxhighlight>. We say it is a "monadic operator called dyadically".


When called monadically, a monadic operator with an array operand has the same syntax as a [[dyadic function]]: in each case, there is one array to the left and one to the right of the operator or function. The difference is that while the value on the right is always an [[argument]], the one on the left is an [[operand]] to the operator but an argument to the function. This means that the operator can be applied in two steps: in most modern dialects the operand and its operator can be separated, for example changing the [[Constant operator]] call <source lang=apl inline>A ⍨ B</source> to <source lang=apl inline>toA←A ⍨ ⋄ toA B</source>, but few allow the array-function combination that would be required to change the equivalent [[Identity]] call <source lang=apl inline>A ⊢ B</source> to <source lang=apl inline>toA←A ⊢ ⋄ toA B</source>. In some dialects this ambiguity is used to allow [[function-operator overloading]], where a value can be either a monadic operator or a dyadic function depending on context.
When called monadically, a monadic operator with an array operand has the same syntax as a [[dyadic function]]: in each case, there is one array to the left and one to the right of the operator or function. The difference is that while the value on the right is always an [[argument]], the one on the left is an [[operand]] to the operator but an argument to the function. This means that the operator can be applied in two steps: in most modern dialects the operand and its operator can be separated, for example changing the [[Constant operator]] call <syntaxhighlight lang=apl inline>A ⍨ B</syntaxhighlight> to <syntaxhighlight lang=apl inline>toA←A ⍨ ⋄ toA B</syntaxhighlight>, but few allow the array-function combination that would be required to change the equivalent [[Identity]] call <syntaxhighlight lang=apl inline>A ⊢ B</syntaxhighlight> to <syntaxhighlight lang=apl inline>toA←A ⊢ ⋄ toA B</syntaxhighlight>. In some dialects this ambiguity is used to allow [[function-operator overloading]], where a value can be either a monadic operator or a dyadic function depending on context.
== Derived monadic operator ==
== Operand currying ==
Some dialects allow [[wikipedia:currying]] one operand to a dyadic operator, creating a monadic operator:
Some dialects allow [[wikipedia:currying|currying]] one operand to a dyadic operator, creating a monadic operator:
<source lang=apl>
<syntaxhighlight lang=apl>
       Thrice←⍣3
       Thrice←⍣3
       1 +Thrice 4
       1 +Thrice 4
7
7
</source>
</syntaxhighlight>
Some dialects only allow currying the right operand.
Some dialects only allow currying the right operand.
{{APL syntax}}
{{APL syntax}}[[Category:Operators]]

Latest revision as of 22:05, 10 September 2022

In APL syntax, a monadic operator (or adverb) is an operator with one operand, which is written to the operator's left. Outer Product is usually also considered a monadic operator, even though it is written with two symbols and the operand on the right. A specified axis may also be considered a monadic operator, even though its syntax is anomalous: it is written with brackets, and can apply to the Reduce and Scan operators even though operators are not usually allowed to be operands.

The term "monadic operator" refers to the valence of the operator itself, that is, the number of operands. When applied, it produces a derived function, which can have a different function valence. For example, the 2-wise reduction 2 =/ 1 2 2 3 illustrates the monadic operator Reduce (/) applied to a single operand =, and then to two arguments 2 and 1 2 2 3. We say it is a "monadic operator called dyadically".

When called monadically, a monadic operator with an array operand has the same syntax as a dyadic function: in each case, there is one array to the left and one to the right of the operator or function. The difference is that while the value on the right is always an argument, the one on the left is an operand to the operator but an argument to the function. This means that the operator can be applied in two steps: in most modern dialects the operand and its operator can be separated, for example changing the Constant operator call A ⍨ B to toA←A ⍨ ⋄ toA B, but few allow the array-function combination that would be required to change the equivalent Identity call A ⊢ B to toA←A ⊢ ⋄ toA B. In some dialects this ambiguity is used to allow function-operator overloading, where a value can be either a monadic operator or a dyadic function depending on context.

Operand currying

Some dialects allow currying one operand to a dyadic operator, creating a monadic operator:

      Thrice←⍣3
      1 +Thrice 4
7

Some dialects only allow currying the right operand.

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