Function: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>")
m (Text replacement - "<source" to "<syntaxhighlight")
 
Line 15: Line 15:
* [[Dyadic]] if only a two-argument call can be valid
* [[Dyadic]] if only a two-argument call can be valid
* [[Ambivalent]] if both cases are possible
* [[Ambivalent]] if both cases are possible
* no name, if neither case is possible (for instance <source lang=apl inline>3∘+∘4</syntaxhighlight>)
* no name, if neither case is possible (for instance <syntaxhighlight lang=apl inline>3∘+∘4</syntaxhighlight>)
while the last case can be produced, it is unlikely to be useful, and almost never discussed.
while the last case can be produced, it is unlikely to be useful, and almost never discussed.


Line 23: Line 23:
Every APL comes with [[primitive function]]s, and almost always [[system function]]s, which are defined by the language. In order to create other functions, there are three general methods. Every APL supports at least one of these but there is no universally supported method.
Every APL comes with [[primitive function]]s, and almost always [[system function]]s, which are defined by the language. In order to create other functions, there are three general methods. Every APL supports at least one of these but there is no universally supported method.
* [[Tacit programming]] treats functions as values to be manipulated directly: [[derived function]]s can be created by applying [[operator]]s to [[operand]]s, and [[train]]s are functions formed from multiple functions.
* [[Tacit programming]] treats functions as values to be manipulated directly: [[derived function]]s can be created by applying [[operator]]s to [[operand]]s, and [[train]]s are functions formed from multiple functions.
* [[Anonymous function]]s are defined by a series of expressions that make reference to function arguments ([[dfn]]s use the fixed names <source lang=apl inline>⍺</syntaxhighlight> and <source lang=apl inline>⍵</syntaxhighlight>). When created they have no name, but they can be [[Assignment|assigned]] one.
* [[Anonymous function]]s are defined by a series of expressions that make reference to function arguments ([[dfn]]s use the fixed names <syntaxhighlight lang=apl inline>⍺</syntaxhighlight> and <syntaxhighlight lang=apl inline>⍵</syntaxhighlight>). When created they have no name, but they can be [[Assignment|assigned]] one.
* [[Defined function]]s have a function header with syntax that includes the function's name and reflects how it will be used. This is the only way to create [[niladic function]]s.
* [[Defined function]]s have a function header with syntax that includes the function's name and reflects how it will be used. This is the only way to create [[niladic function]]s.



Latest revision as of 22:19, 10 September 2022

In APL syntax, a function (or verb) applies to array arguments to produce an array result (although some APLs allow functions to return other syntactic elements). Unlike many programming languages which use parentheses or brackets for function calls, function application in APL is written simply by writing the function next to its arguments. Functions may be primitive functions, derived functions (including trains), or defined functions. They can be used as operands to operators; in early APLs operands were always functions, but nearly all modern APLs allow array operands, for example to the Power operator.

A key feature distinguishing APL from functional programming languages such as Lisp is that APL functions are not first class objects: they cannot be passed directly as arguments to other functions like arrays. Instead, functions might be considered "second class" objects while operators are "third class" and hyperators, if present, are "fourth class" or more. In a few APLs, most prominently A+, a mechanism is provided to pass functions as arguments, making functions first class if a special function call syntax is used.

Function valence

Functions may take zero, one, or two arguments when applied (to pass more arguments, combine them into a single list argument). Which of these possibilities applies is known as the function's valence:

  • A niladic function takes no arguments: it is evaluated immediately when execution reaches it.
  • A monadic function or prefix function takes one argument on its right.
  • A dyadic function or infix function takes two arguments: a left argument and a right argument.

The monadic and dyadic cases are distinguished during execution by checking to see whether there is an array to the function's left, but there is no way to determine from context whether a function should be niladic. It must be a property of the function itself. Thus functions are divided into niladic functions, which are always evaluated immediately with no arguments, and non-niladic functions, which may be monadic or dyadic. Niladic functions lack some of the properties expected of functions—they cannot be passed as operands and cannot be produced as derived functions—so they are often considered to be a special kind of syntactic element rather than a function, and "function" sometimes indicates a non-niladic function.

APL's syntax allows a non-niladic function to be called either monadically or dyadically. However, one or both of these cases may give a SYNTAX ERROR regardless of the argument. In this case the function is called

  • Monadic if a one-argument call can be valid but a two-argument call cannot
  • Dyadic if only a two-argument call can be valid
  • Ambivalent if both cases are possible
  • no name, if neither case is possible (for instance 3∘+∘4)

while the last case can be produced, it is unlikely to be useful, and almost never discussed.

Function definition

Main article: Function styles

Every APL comes with primitive functions, and almost always system functions, which are defined by the language. In order to create other functions, there are three general methods. Every APL supports at least one of these but there is no universally supported method.

More recent APLs tend to use the earlier, syntactically simpler, methods on the list above, while older APLs use syntactically more complex ones: in particular, only defined functions were used in almost all APLs until the 1990s. Because they create functions as values with no names, the first two types require function assignment to create named functions (usually considered a requirement to build complex programs). Function assignment became more common in the 1980s, and, due to the popularity of anonymous functions, is present in all new APLs.


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