Function: Difference between revisions

Jump to navigation Jump to search
150 bytes removed ,  22:19, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
mNo edit summary
m (Text replacement - "<source" to "<syntaxhighlight")
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
In [[APL syntax]], a '''function''' applies to [[array]] [[argument]]s 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 function]]s, [[derived function]]s (including [[train]]s), or [[defined function]]s. They can be used as [[operand]]s to [[operator]]s; in early APLs operands were always functions, but nearly all modern APLs allow array operands, for example to the [[Power operator]].
In [[APL syntax]], a '''function''' (or '''verb''') applies to [[array]] [[argument]]s 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 function]]s, [[derived function]]s (including [[train]]s), or [[defined function]]s. They can be used as [[operand]]s to [[operator]]s; 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 [[wikipedia:functional programming|functional programming]] languages such as [[wikipedia:Lisp (programming language)|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 [[operator]]s are "third class" and [[hyperator]]s, 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.
A key feature distinguishing APL from [[wikipedia:functional programming|functional programming]] languages such as [[wikipedia:Lisp (programming language)|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 [[operator]]s are "third class" and [[hyperator]]s, 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.
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</source>)
* 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 21: Line 21:
{{main|Function styles}}
{{main|Function styles}}


Every APL comes with [[primitive function]]s, and almost always [[system function]]s, which are defined by the language. [[Derived function]]s can also be created by applying [[operator]]s to [[operand]]s. For example <source lang=apl inline>+/</source>, [[Plus]] [[Reduce]], is a derived function that can be created in any APL. However, early APLs usually did not allow [[assign]]ing a name to such functions: the only ways to call them were to write the function out explicitly and to define an equivalent [[defined function]]. Function assignment became more common in the 1980s, and after the advent of [[tacit programming]] in the 90s it is present in all new APLs.
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.
* [[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.


The traditional way of defining functions in APL, and the most common way to define functions currently, is to create a [[defined function]], which consists of a sequence of APL statements. This is the only way to create a [[niladic function]], although not all ways to define a function support creating niladic functions. There are three major families of function definition:
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 function]]s 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.
* [[Direct definition]], introduced by [[Ken Iverson]] in [[A Programming Language]] but rarely implemented. Direct definition might be considered a kind of APL [[wikipedia:pseudocode|pseudocode]].
* [[Tradfn]]s (often simply called "defined functions"), introduced by [[APL\360]]. In these functions, a [[function header|header]] line describes the way the function is called and the result is placed in a variable whose name is given in the header.
* [[dfn]]s, developed by [[John Scholes]] and introduced in [[Dyalog APL]]. In a dfn, the arguments are given the fixed names <source lang=apl inline>⍺</source> and <source lang=apl inline>⍵</source>, and the result of the first non-assignment statement is returned.


{{APL syntax}}
{{APL syntax}}[[Category:Functions| ]]

Navigation menu