Ambivalent function

From APL Wiki
Revision as of 14:42, 30 April 2020 by Marshall (talk | contribs) (Kinds of functions category)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

In APL syntax, a function is ambivalent, or variadic, if it can be called with either monadic or dyadic valence. While every non-niladic function can be written with one or two arguments, some functions give a SYNTAX ERROR when called with two arguments (these functions are called monadic) or with only one (these are called dyadic). Ambivalent functions do not give a SYNTAX ERROR immediately in either case.

Being ambivalent, or not, is an inherent property of a function. This contrasts with the terms "monadic" and "dyadic" which may refer either to the function's inherent properties as described above or to the context in which it is used. When an ambivalent function is invoked it has either one argument or two: we would say for example it is an ambivalent function called monadically.


In some APLs, for example APL*PLUS, tradfns that declare a left argument in the function header are always ambivalent, and the code needs to check whether a left argument was supplied by the left argument name's name class; 0 ⎕NC 'leftArg'. Other APLs, for example Dyalog APL, will not allow calling such a function monadically, unless the header has curly braces around the left argument name to indicate that it is optional; result {leftArg} FnName rightArg. Then too, they need to query the name class, or, unique for Dyalog APL, use a specialised Called Monadically I-beam, 900.[1]


Dfns are always ambivalent, even if they reference . (This is in contrast with dops which have a fixed operator valence determined by the presence of an unquoted ⍵⍵ in their code.) Like tradfns, the dfn can check the name class (0 ⎕NC '⍺').

However, most APLs that support dfns also allow a statement of the structure defaultValue which skipped if the function was called dyadically.[2] This syntax also allows assigning a function, or even an operator, to , thus streamlining many ambivalent definitions. For example, we can create a Nth root function which defaults to being the square root when used monadically:

Try it online!

     * ÷

We can also define the Over operator with defaulting to be an operator that skips its operand:

Try it online!

    {  ⍺⍺}
    (⍵⍵ ) ⍺⍺ (⍵⍵ )


  1. Dyalog APL Language Reference Guide > The I-Beam Operator > Called Monadically
  2. Dyalog APL Programming Reference Guide > Defined Functions & Operators > Direct Functions & Operators > Default Left Argument

APL syntax [edit]
General Comparison with traditional mathematicsPrecedenceTacit programming
Array Numeric literalStringStrand notationObject literalArray notation
Function ArgumentFunction valenceDerived functionDerived operatorNiladic functionMonadic functionDyadic functionAmbivalent functionTradfnDfnFunction train
Operator OperandOperator valenceTradopDopDerived operator
Assignment MultipleIndexedSelectiveModified
Other Function axisBranchingSystem commandUser commandKeywordDot notationFunction-operator overloading