Misplaced Pages

APL (programming language)

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

This is an old revision of this page, as edited by ABCD (talk | contribs) at 03:41, 4 November 2004 (Overview). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Revision as of 03:41, 4 November 2004 by ABCD (talk | contribs) (Overview)(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

APL (for A Programming Language, or sometimes Array Processing Language) is an array programming language invented in 1962 by Kenneth E. Iverson while at Harvard University. Iverson received the Turing Award in 1979 for his work. As with all programming languages that have had several decades of continuous use, APL has changed significantly from the original language described by Iverson in his book A Programming Language in 1963. One thing that has remained constant is the interpretive nature of the APL programming environment, a feature much appreciated by its users. Conversely, its initial lack of support for both structured and modular programming has been solved by all the modern APL incarnations. One much criticized aspect still remains, though: the use of a non-standard character set (see Character set below.)

Overview

Over a very wide set of problem domains (math, science, engineering, computer design, robotics, data visualization, actuarial science, traditional DP, etc.) APL is an extremely powerful, expressive and concise programming language, typically set in an interactive environment. It was originally created as a way to describe computers, by expressing mathematical notation in a rigorous way that could be interpreted by a computer. It is easy to learn but APL programs can take some time to understand. Unlike traditional structured programming languages, code in APL is typically structured as chains of monadic or dyadic functions and operators acting on arrays. Because APL has so many nonstandard operators, APL does not have function or operator precedence. The original APL did not have control structures (loops, if-then-else), but the array operations it included could simulate structured programming constructs. For example, the iota operator (which yields an array from 1 to N) can simulate for-loop iteration.

The APL environment is called a workspace. In a workspace the user can define programs and data, i.e. the data values exists also outside the programs, and the user can manipulate the data without the necessity to define a program, for example:

N 4   5   6   7 {\displaystyle N\leftarrow 4\ 5\ 6\ 7}

Assign the vector values 4 5 6 7 to N.

N + 4 {\displaystyle N+4\,\!}

Print the values 8 9 10 11

+ / N {\displaystyle +/N\,\!}

Print the sum of N, i.e. 22


The user can save the workspace with all values and programs. In any case, the programs are not compiled but interpreted.

APL is notorious for its use of a set of non-ASCII symbols that are an extension of traditional arithmetic and algebraic notation. These cryptic symbols, some have joked, make it possible to construct an entire air traffic control system in two lines of code. Indeed, in some versions of APL, it is theoretically possible to express any computable function in one expression, that is in one line of code. You can use the other line for I/O, or constructing a GUI. Because of its condensed nature and non-standard characters, APL has sometimes been termed a "write-only language", and reading an APL program can feel like decoding an alien tongue, except to mathematicians, who find more prolix languages much harder to understand. Because of the unusual character set, many programmers used special APL keyboards in the production of APL code. Nowadays there are various ways to write APL code using only ASCII characters.

Iverson designed a successor to APL called J which uses ASCII "natively". So far there is a sole single source of J implementations: http://www.jsoftware.com/ Other programming languages offer functionality similar to APL. A+ is an open source programming language with many commands identical to APL.

Here's how you would write a program that would sort a word list stored in vector X according to word length:

X

Here's a program that finds all prime numbers from 1 to R:

( R R . × R ) / R 1 ι R {\displaystyle \left(\sim R\in R\circ .\times R\right)/R\leftarrow 1\downarrow \iota R}

Here's how to read it, from right to left:

  1. ι {\displaystyle \iota \,\!} creates a vector containing integers from 1 to R (if R = 6 at the beginning of the program, ι R {\displaystyle \iota R\,\!} is 1 2 3 4 5 6)
  2. Drop first element of this vector ( {\displaystyle \downarrow } function), i.e. 1. So 1 ι R {\displaystyle 1\downarrow \iota R} is 2 3 4 5 6
  3. Set R to the vector ( {\displaystyle \leftarrow } , assignment operator)
  4. Generate outer product of R multiplied by R, i.e. a matrix which is the multiplication table of R by R ( . × {\displaystyle \circ .\times } function)
  5. Build a vector the same length as R with 1 in each place where the corresponding number in R is in the outer product matrix ( {\displaystyle \in } , set inclusion function), i.e. 0 0 1 0 1
  6. Logically negate the values in the vector (change zeros to ones and ones to zeros) ( {\displaystyle \sim } , negation function), i.e. 1 1 0 1 0
  7. Select the items in R for which the corresponding element is 1 ( / {\displaystyle /\,\!} function), i.e. 2 3 5

Here's the equivalent in Perl (another "write-only" language):

perl -le '$_ = 1; (1 x $_) !~ /^(11+)\1+$/ && print while $_++'

Character set

APL has always been criticized for its choice of a unique, non-standard character set. The fact that those who learn it usually become ardent adherents shows that there is some weight behind Iverson's idea that the notation used does make a difference. With the popularization of the Unicode standard, which contains an APL character set, the eternal problem of obtaining the required particular fonts seems poised to go away. Nevertheless, some critics attack not the use of a symbolic font, but rather the specific "purely circumstantial" choices that were made during the early implementation of APL, driven by the availability of a special kind of typewriter that would never become mainstream.

Quotes

"APL, in which you can write a program to simulate shuffling a deck of cards and then dealing them out to several players in four characters, none of which appear on a standard keyboard."
David Given

See also

  • J: APL's successor, by Iverson and Hui
  • K: an alternative APL successor, by Arthur Whitney
  • NGL: a descendant of APL (through J and FL), using more standard math-like notation
  • Nial


References

External links

APL symbols and keyboard layout

APL keyboard with special characters that want to play with you
APL keyboard with special characters that want to play with you

Note the mnemonics associating an APL character with a letter : question mark on Q, power on P, rho on R, base value on B, eNcode on N, modulus on M and so on. This makes it easier to type APL on a non-APL keyboard providing you have visual feedback on your screen.

All APL symbols are present in Unicode:
It may be required to significantly reconfigure your browser in order to display Unicode fonts.

' ( ) + , - . / : ; < = > ? [ ]
\ _ ¨ ¯ × ÷

Categories:
APL (programming language) Add topic