Readability: Difference between revisions

Jump to navigation Jump to search
1,953 bytes added ,  09:18, 27 September 2020
no edit summary
No edit summary
Line 1: Line 1:
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.
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. Since [[Phil Abrams]] used the term at the [[APL '73]] conference,<ref>Abrams, Phil. ''Program Writing, Rewriting and Style''. APL Conference 73. Canadian Printco Limited. 1973.</ref> APLers have traditionally used ''pornography'' to describe code that is hard to read, or uses unusual constructs. [[Alan Perlis]] countered that ''But as we all know, being people of the world, pornography thrives!''<ref>Perlis, Alan. [https://www.jsoftware.com/papers/perlis78.htm Almost Perfect Artifacts Improve only in Small Ways: APL is more French than English]. [[APL '78]].</ref> [[Code golf]] often results in pornographic code.
== Examples ==
== Examples ==
=== Gilman & Rose ===
=== Gilman & Rose ===
In [[Books#APL_.E2.80.95_An_Interactive_Approach|APL ― An Interactive Approach]], the authors describe the following code, which computes the correlation coefficient, as “almost pornographic”:<!--- For the effect, this is intentionally not syntax coloured --->
In [[Books#APL_.E2.80.95_An_Interactive_Approach|APL ― An Interactive Approach]], the authors describe the following code, which computes the correlation coefficient, as “almost pornographic”:
:<code>r←(+/x×y)÷((+/(x←x-(+/x)÷⍴x)*2)×+/(y←y-(+/y)÷⍴y)*2)×.5</code>
:<source lang=none inline>r←(+/x×y)÷((+/(x←x-(+/x)÷⍴x)*2)×+/(y←y-(+/y)÷⍴y)*2)×.5</source>
By splitting the expression intro even a moderate number of pieces, a symmetry is revealed:
By splitting the expression intro even a moderate number of pieces, a symmetry is revealed:
<pre>yVar←+/(y-(+/y)÷⍴y)*2
<source lang=none>yVar←+/(y-(+/y)÷⍴y)*2
xVar←+/(x-(+/x)÷⍴x)*2
xVar←+/(x-(+/x)÷⍴x)*2
r←(+/x×y)÷(xVar×yVar)×0.5</pre>
r←(+/x×y)÷(xVar×yVar)×0.5</source>
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 ([[wikipedia:variance|variance]]). Finally, the <source lang=apl inline>.5</source> is expanded to <source lang=apl inline>0.5</source> which helps to clarify that this is a decimal number and not an [[inner product]].
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 ([[wikipedia:variance|variance]]). Finally, the <source lang=apl inline>.5</source> is expanded to <source lang=apl inline>0.5</source> which helps to clarify that this is a decimal number and not an [[inner product]].


Line 17: Line 17:


=== IBM ===
=== IBM ===
The [[idiom]] list included with [[APL2]] includes the following entry:<ref>Cason, Stan. [ftp://ftp.software.ibm.com/ps/products/apl2/info/APL2IDIOMS.pdf#page=3 APL2 IDIOMS Library]. IBM.</ref>
The [[idiom]] list included with [[APL2]] includes the following entry:<ref>Cason, Stan. [ftp://ftp.software.ibm.com/ps/products/apl2/info/APL2IDIOMS.pdf#page=3 APL2 IDIOMS Library], Assignment Algorithms. IBM.</ref>
:{| style=width:100%
:{| style=width:100%
|<source lang=apl inline>X←'line1',0⍴Y←'line2'</source>||<source lang=apl inline>⍝ Pornography. Combining two lines into one.</source>
|<source lang=apl inline>X←'line1',0⍴Y←'line2'</source>||<source lang=apl inline>⍝ Pornography. Combining two lines into one.</source>
Line 57: Line 57:
</source>
</source>
A new [[namespace]], with the original value of <source lang=apl inline>ns</source> as name, is created inside <source lang=apl inline>container</source> and the character representation <source lang=apl inline>'#.container.ns'</source> is returned from <source lang=apl inline>⎕NS</source> to <source lang=apl inline>⍎</source> which evaluates the name to a reference, that in turn replaces the previous value of <source lang=apl inline>ns</source>. Note that <source lang=apl inline>⎕NS</source> returns fully qualified namespace path to the newly created namespace, and thus it doesn't matter in which namespace <source lang=apl inline>⍎</source> is called.
A new [[namespace]], with the original value of <source lang=apl inline>ns</source> as name, is created inside <source lang=apl inline>container</source> and the character representation <source lang=apl inline>'#.container.ns'</source> is returned from <source lang=apl inline>⎕NS</source> to <source lang=apl inline>⍎</source> which evaluates the name to a reference, that in turn replaces the previous value of <source lang=apl inline>ns</source>. Note that <source lang=apl inline>⎕NS</source> returns fully qualified namespace path to the newly created namespace, and thus it doesn't matter in which namespace <source lang=apl inline>⍎</source> is called.
=== Honeywell ===
[[Honeywell]] <ref>Honeywell. [http://www.softwarepreservation.org/projects/apl/Books/198512_Multics%20APL%20Users%20Guide_AK95-02.pdf#page=106 Multics APL User's Guide] (AK95-02), 3-16. December 1985.</ref> used a more specific definition:
:In APL, <u>pornography</u> is defined informally as the dependence upon undefined evaluation order for the successful or correct evaluation of an APL statement.
This refers to things like
<source lang=apl>
a←4
(a+3)×a
</source>
where it is undefined whether the initial value for <source lang=apl inline>a</source> is used at all in the second line, yielding 12, or whether the second assignment is done before [[times]] gets its right argument, and thus the result is 9.


Similarly, in
<source lang=apl>
i←2
(2 1⍴10 20)[i;i←1]
</source>
the evaluation order of the statements in the [[bracket indexing]] is undefined. If <source lang=apl inline>i</source> is evaluated before <source lang=apl inline>i←1</source> then the result is 20, otherwise it is 10.
The Multics APL manual goes on to use the terms ''monstrosity'' and ''eyesore'' for code published in an APL newsletter, such as
:<source lang=none inline>Z[B+(C∧X∊D)/⍳⍴X;]+(24p' Y9  X9 ')[(C←(-≠\''''=X)∧A≤⍴D)/A←(D←'⍵⍺')⍳X;]</source>
The manual suggests that this code should be split into the following expressions:
<source lang=apl>
D←'⍵⍺'
A←D⍳X
C←(-≠\''''=X)∧A≤⍴D
B←(C∧X∊D)/⍳⍴X
Z[B;]←(24p' Y9  X9 ')[C/A;]
</source>
== See also ==
== See also ==
* [[Semantic density]]
* [[Semantic density]]

Navigation menu