3 ms·
I do not agree. I have never used an APL interpreter or compiler, but nonetheless I use it as a pseudo-code, so instead of no one, there is at least one user.
by adrian_b 5y ago
I do not agree. I have never used an APL interpreter or compiler, but nonetheless I use it as a pseudo-code, so instead of no one, there is at least one user.
Whenever people claim that APL is write-only they give some convoluted example with a bunch of obscure APL operators that are seldom used, so even for APL users it might be difficult to remember what they do.
For the majority of the problems that a programmer might need to solve every day, only a small fraction of APL might be needed, which is simple to understand, learn and remember.
As an unfamiliar notation, you might need to learn 5 or 6 extra operators at most and for the rest you need just use the same that you use in any programming language only with additional rules that allow more freedom than in the popular programming languages.
The main difference between APL and conventional languages is that you no longer have to write "for" loops, you just write the same expressions that you would write for scalar variables.
I cannot see how one would claim that omitting all the redundant boilerplate code needed by "for" loops or "map" functions decreases the readability of a program.
If you do not want to learn 20 new operators, there is no need for that, the base APL language does not need them. There are only very few operators that are strictly needed, e.g. reduction, inner product, outer product, rank, index.
If you like verbose identifiers you could use names for them, the symbols are not the essence of APL, only its rules for writing expressions.
The 3 simplest rules are:
1. a function of 1 argument applied to an array is the array of the values obtained by applying the function to the array elements.
2. a function of 2 arguments applied to a scalar and to an array is the array of the values obtained by applying the function to the scalar and to each array element.
3. a function of 2 arguments applied to 2 arrays of identical ranks and dimensions is the array of the values obtained by applying the function to each pair of corresponding array elements.
I do not see anything obscure and unintuitive in such simple rules. (It should be obvious that these are particular cases of a general rule that in a function of N scalar arguments, you should be able to replace 1 or more of the scalars with arrays of the same rank and dimensions and then the function result shall also be an array of the same rank and dimensions.)
On the contrary I find it hard to understand why these rules are not valid in any programming language and you are forced in most of them to write loops for such trivial operations.
There is only one real barrier for using the APL notation, but in my opinion those who surpass it are rewarded.
The order of evaluation for APL expressions is different from the order learned in school, which is then extended in various way in the programming languages.
In APL there are no precedence levels. The right operand of an operator is everything that is to the right, until the end of the expression.
So the operators are evaluated from the right to the left (i.e. in reverse order compared to the traditional order for e.g. addition), unless you use parentheses. This order is frequently more readable than the traditional, because usually when browsing a program you do not need to read the expressions until their end. The important parts are at the left end and it is normally enough to read only those. With the traditional order you might need to scan the line much farther to the right to be sure that you did not miss something important.
This obstacle of the different evaluation order discourages many of those who could benefit by using APL as a pseudo-code, but once you understand that this modified order can really simplify most expressions and you become adept at reading it as easily as the traditional order, you can use APL as a pseudo-code with good results.
- civilized 5y agoThanks for these interesting thoughts. One quick question: how is rule 2 a special case of the general rule? I think I'm missing something in your explanation of rule 2. Is it just the scalar+array version of rule 3, for a function f(x, y) of two scalar arguments? Could you then say that one of the big ideas of APL is to define a canonical way of extending functions of scalars f(x, y, ...) to take one or more array arguments, and then baking those ways into the syntax, so you don't have to use zip and map? I agree there's something to that. It seems like a feature that other languages could benefit from. Maybe a decorator symbol could be used to tell the language when you want to do this, or a setting so it's turned on by default in a particular module. We probably wouldn't want it as an immutable universal language rule, since entering an array to scalar argument is a common programming mistake that you could want your compiler or runtime to throw an error for.
- adrian_b 5y agoThe rules 2 and 3 are how to derive a function with array arguments from a function with 2 scalar arguments. You can replace one scalar with an array by rule 2 and both scalars with arrays by rule 3. This should be extended to an arbitrary number of scalar arguments, where you should be able to replace any scalar with an array whose elements have the same type as the scalar and any combination of scalar and array arguments should be valid. This is the first step in using APL, by defining only scalar functions, but using them also with array arguments. This only works with arrays of the same dimensions, where the function arguments are elements in the same positions, i.e. which would have had the same indices in a "for" loop. This already covers the most frequent uses. Only when you need more complex ways of combining arrays, you begin to need specific APL operators, e.g. reduction which transforms an array into an array whose rank is 1 less, e.g. like when you sum the elements of a vector into a scalar value, but the APL operators are completely general, so with the reduction operator you can apply any function of 2 arguments to a vector or to the subvectors of a larger array. Reduction operations are one of the places where it is obvious that the APL order of evaluation is better than the traditional, because it provides useful operations for the non-commutative functions, e.g. reduction with subtraction is the sum with alternate signs and reduction with division is the product of the fractions obtained from pairs of consecutive elements. Both operations are frequently useful, unlike to what would have been obtained with the traditional order of evaluation. The same happens with the other APL operators, they are generalized versions of the traditional operations, e.g. the inner product operator can do dot products or matrix multiplications, but it can be used with any pair of functions, not only with addition and multiplication. Like any use of overloaded functions, you are right that this would no longer flag as an error when you use an array of e.g. int instead of an int, but usually this would be caught eventually, e.g. if you try to assign the result to a scalar, unless you do this in an initialization with auto inference of the declared type, which would probably still give a type conflict later. I agree that this is a possible disadvantage, but I have never encountered an error of this kind, with overloaded functions or operators, that was not flagged eventually as a type error, even if possibly not in the first place where the error was introduced.