APL programming language

HomePage | Recent changes | View source | Discuss this page | Page history | Log in |

Printable version | Disclaimers | Privacy policy

The APL (A Programming Language) language was invented in 1962 by Kenneth Iverson while at Harvard University. Iverson received the Turing Award in 1980 for his work.

APL is an extremely powerful, expressive and concise programming language. It was originally created as a way to express 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. APL has been called a write-only language. The original APL did not have control structures (loops, if-then-else). APL systems are typically interactive.

APL is renowned for using a set of non-ASCII symbols that are an extension of traditional arithmetic and algebraic notation and being able to express an air traffic control system in two lines of code. Reading an APL program can feel like decoding an alien tongue. However, there are now 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/

"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

Here's how you would write a "Hello Word" program in APL:

'Hello World'

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

X[X+.¬' ';] 

Here's a program that find all prime numbers from 1 to N:

(.tilde N .contains N .cirle . .product N)/N .leftarrow 1 .downarrow .iota N

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

1. .iota N creates a vector containing integers from 1 to N (if N = 6 at the beginning of the program, .iota N is {1, 2, 3, 4, 5, 6}

2. Drop first element of this vector (.downarrow function), i.e. 1. So 1 .downarrow .iota N is {2, 3, 4, 5, 6}

3. Set N to the vector (.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 (.circle . .product function)

5. Build a vector the same length as N with 1 in each place where the corresponding number in N is in the outer product matrix (.contains, 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) (.tilde, negation function), i.e. {1, 1, 0, 1, 0}

7. Select the items in N for which the corresponding element is 1 (slash 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 $_++'