From APL Wiki
Revision as of 08:18, 27 September 2020 by Adám Brudzewsky (talk | contribs)
Jump to navigation Jump to search

Maintaining readability of APL can take a special effort. It is easy to write very dense code, and the mathematical look of APL can encourage usage of single-letter names. Traditionally, APLers use the term pornography to describe code that is hard to read, or uses unusual constructs. Code golf often results in pornographic code.


Gilman & Rose

In APL ― An Interactive Approach, the authors describe the following code, which computes the correlation coefficient, as “almost pornographic”:


By splitting the expression intro even a moderate number of pieces, a symmetry is revealed:


This also avoids reusing variable names, and thus ensures that the code can be rerun from any point. The chosen additional variable names are still short, but quite indicative of what they signify (variance). Finally, the .5 is expanded to 0.5 which helps to clarify that this is a decimal number and not an inner product.

A more modern approach breaks out the symmetry into a utility function train, and uses leading axis theory combined with operators and reordering of terms to avoid parentheses (which would otherwise require a mental stack to understand). Finally, the correlation coefficient is defined as a stand-alone function, using inner product to combine summation with multiplication

r←x R y

Note that +⌿÷≢ is an idiom (common phrase) and is read as average by even moderately experienced APL programmers.


The idiom list included with APL2 includes the following entry:[1]

X←'line1',0⍴Y←'line2' ⍝ Pornography. Combining two lines into one.

This was once a common technique before, even though it is prone to fail in where the value to the left of ,0⍴ isn't a vector, for example in the following example where X becomes a 1-element vector instead of the intended scalar:


With the addition of Left () to the language, this type of hack became became entirely obsolete:

X←'line1' ⊣ Y←'line2'

The primitive leaves its left argument unmodified:

      X←'l' ⊣ Y←'line2'
1 0 0 0 0

The Diamond statement separator () provides an alternative means of inlining multiple statements:

Y←'line2' ⋄ X←'line1'

Note that in all the above, Y is assigned first.

Morten Kromberg

Morten Kromberg asked one of his colleagues to “Please avoid this kind of pornography:”


Avoiding the unusual modified assignment (using the 2-train ⍎⎕NS as modifying function) helps:

ns←ns container.(⍎⎕NS) ⍬

Finally, splitting the 2-train apart makes it even clearer:

ns←⍎ns container.⎕NS ⍬

A new namespace, with the original value of ns as name, is created inside container and the character representation '#.container.ns' is returned from ⎕NS to which evaluates the name to a reference, that in turn replaces the previous value of ns.

See also


  1. Cason, Stan. APL2 IDIOMS Library. IBM.
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 structure