APL a Day #1: Functions
In early grade school mathematics, students already learn the concept of a function, but they do not know what they are called. An expression like
5+5 uses the function known as addition. The addition function takes two numbers and describes a new value in terms of the two old ones. In this case,
5+5 describes the sum of five and five, which is also known as 10. Another way of saying this is that
5+5 is the plus function applied to five and five. This series will prefer to talk about functions as describing new values in terms of old values, rather than the more common practice in Computer Science of saying that a function is applied to its arguments.
Throughout most of early mathematics education, all functions introduced appear between their arguments when written, such as
(3+3)÷5. Furthermore, all functions early on take exactly two arguments, one which appears on the left side and one that appears on the right. In more advanced mathematics, names such as f and g are often used to represent some function which is later given some specific meaning. Usually, these functions take a single argument on their right side, surrounded in parentheses. Functions are defined in terms of their argument, such as the following definition:
f(x) = x*2
In this definition, the function f describes the square of its right argument, which is given the name x in the right side of the equation. Using this syntax, one writes
f(f(x)) to describe the square of the square of x, or
In APL, functions of a single argument, like f above, take their argument on the right, just as in the above example. Indeed, in APL, writing
f(5) would describe the square of 5 just as in regular mathematics notation if one f defines to mean the same thing as in the above example. However, in APL, the parentheses need not appear. Instead, the expressions
f 5 mean the same thing, that is, they both describe the square of 5. Thus, the expression
f f 5 also means the square of the square of 5 in APL.
Normal math notation often treats functions like
f(x, y) as the function f applied to two arguments. Instead, treat this as a function of one argument that is split into two values, x and y. In this case, it is also correct to write
f(x, y) in APL, and it means the same thing. However, just like above, the parentheses do not serve a purpose, and thus
f x y means the same thing. (Strictly speaking, this equivalency holds only for scalar values, but a later session will clarify this distinction.)
Functions like f above are monadic: they take a single right argument. Functions like
+ are dyadic: they take a right argument and an additional left argument. In standard math notation, either a symbol is used between two arguments, that is, dyadically, or a function takes a single right argument surrounded in parentheses. There are additional cases like
|x| where the function surrounds the argument, but these can easily be converted to something like
|x to mean the same thing. In APL, all functions use the exact same notation, the traditional notation of math, with the function appearing between two arguments or on the left side of a single argument, that is, with the single argument on the right or with the two arguments on the left and right. Take for example this normal math expression:
In APL, write this instead as:
Notice that the function f is not given a left argument, and thus, f appears monadically, while + appears dyadically.
Mathematics also defines a complex notion of precedents and orderings of which functions operate first, that is, which values described by functions are the arguments to which other functions. In APL, this rule is simple, the right argument to a function is everything to its right, and the left argument is the single value to the immediate left. Parentheses can be used to enforce a certain ordering just like in math. Consider the following:
In normal math, precedence demands that the
÷ function take precedence, receiving 3 and 3. In APL, the
+ is the rightmost function and thus is first. Parentheses around the expressions clarifies the APL interpretation:
So, to translate
f(x)+x into APL, write
(f x)+x instead of
f x + x which in normal math is
f(x + x) and surely not the desired translation.
Thus, to summarize, given a function f and two values x and y,
x f y describes a dyadic computation named f in terms of the left argument x and the right argument y. Similarly, the expression
f x describes a monadic computation named f in terms of the right argument x. All APL expressions are interpreted or evaluated from right to left, so that the value described by the rightmost function and its arguments is the right argument to the next most rightmost function.
Don't worry if this first session seems rather abstract. Like in mathematics, functions underlie pretty much every other concept, except for the concept of values. In APL, the function syntax is simpler and more consistent than in that of normal mathematics notation, and future sessions will give more than adequate practice in using a variety of functions.