Jump to content

APL (programming language)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 68.191.50.39 (talk) at 15:44, 5 August 2006 (Quotes). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

APL (for A Programming Language) is an array programming language based on a notation invented in 1957 by Kenneth E. Iverson while at Harvard University. It originated in an attempt to provide consistent notation for the teaching and analysis of topics related to the application of computers. The notation was later used to describe the IBM System/360 machine architecture, a description much more concise and exact than the existing documentation and revealing several previously unnoticed problems. Then, a Selectric typeball was made to write a linear representation of this notation. In 1964, a subset of the notation was implemented as a programming language.

Iverson published his notation in 1962 in a book titled A Programming Language and APL got its name from the title of this book. Iverson received the Turing Award in 1979 for his work. As with all programming languages that have had several decades of continual use, APL has changed significantly from the notation described by Iverson in his book. One thing that has remained constant is that APL is interpretive and interactive, features 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 of APL is the use of a special character set (see Character set below.) These characters have all been incorporated into Unicode, which is now the base character set of several APL products. Iverson later reworked APL into a language called J which not only greatly increases expressive power but also uses pure ASCII instead of a special character set.

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 primitives (functions, operators, or features built into the language and indicated by a single symbol or a combination of a few symbols), 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 function (which yields an array from 1 to N) can simulate for-loop iteration. More recent implementations of APL generally include explicit control structures, so that data structure and control-flow structure can be more clearly separated.

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

Assign the vector values 4 5 6 7 to N.

Add 4 to all values (giving 8 9 10 11) and Print them (absence of the assignment arrow means "show").

Print the sum of N, i.e. 22.

The user can save the workspace with all values, programs and execution status. In any case, the programs are usually not compiled but interpreted. A commercial compiler was brought to market by STSC (now Manugistics).

APL is well-known 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 at first feel like decoding an alien tongue. 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. Indeed most if not all modern implementations use the standard keyboard, displaying APL symbols by use of a particular font.

Advocates of APL claim that the examples of "write-only" code are almost invariably examples of poor programming practice or novice mistakes, which can occur in any language.

APL has perhaps had an unusually high percentage of users who are subject-matter experts who know some APL, rather than professional programmers who know something about a subject.

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

Examples

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

File:APL Example 1.gif

Here's a program that finds all prime numbers from 1 to R in time and space both O(R2):

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

  1. creates a vector containing integers from 1 to R (if R = 6 at the beginning of the program, is 1 2 3 4 5 6)
  2. Drop first element of this vector ( function), i.e. 1. So is 2 3 4 5 6
  3. Set R to the vector (, assignment primitive)
  4. Generate outer product of R multiplied by R, i.e. a matrix which is the multiplication table of R by R (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 (, 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) (, negation function), i.e. 1 1 0 1 0
  7. Select the items in R for which the corresponding element is 1 ( function), i.e. 2 3 5

Calculation

APL was unique in the speed with which it could perform complex matrix operations. For example, a very large matrix multiplication would take only a few seconds on a machine which was much less powerful than those today. There were some technical and other economic reasons for this advantage:

  • Commercial interpreters delivered highly-tuned linear algebra library routines.
  • Very low interpretive overhead was incurred per-array—not per-element.
  • APL response time compared favorably to the runtimes of early optimizing compilers.

A widely cited paper "The APL Machine" perpetuated the myth that APL made pervasive use of lazy evaluation where calculations would not actually be performed until the results were needed and then only those calculations strictly required. Although this technique was used by just a few implementations, it embodies the language's best survival mechanism: not specifying the order of scalar operations. Even as eventually standardized by X3J10, APL is so highly data-parallel, it gives language implementers immense freedom to schedule operations as efficiently as possible. As computer innovations such as cache memory, and SIMD execution became commercially available, APL programs ported with little extra effort spent re-optimizing low-level details.

Terminology

APL makes a clear distinction between functions and operators. Functions take values (variables or constants or expressions) as arguments, and return values as results. Operators take functions as arguments, and return related, derived, functions as results. For example the "sum" function is derived by applying the "reduction" operator to the "addition" function. Applying the same reduction operator to the "ceiling" function (which returns the larger of two values) creates a derived "maximum" function, which returns the largest of a group (vector) of values. In the J language, Iverson substituted the terms 'verb' and 'adverb' for 'function' and 'operator'.

APL also identifies those features built into the language, and represented by a symbol, or a fixed combination of symbols, as primitives. Most primitives are either functions or operators. Coding APL is largely a process of writing non-primitive functions and (in some dialects of APL) operators. However a few primitives are considered to be neither functions nor operators, most noticeably assignment.

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. In the beginning, there were few terminal devices which could reproduce the APL character set — the most popular ones employing the IBM Selectric print mechanism along with a custom type element. 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.

APL symbols and keyboard layout

File:APL keyboard.gif
APL keyboard with special characters

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 for an English-language speaker to type APL on a non-APL keyboard providing one has visual feedback on one's screen.

A more up to date keyboard diagram, applicable for APL2 and other modern implementations, is available: Union layout for windows.

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

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

See also

Usage

APL has long had a small but fervent user base. It has been particularly popular in financial and insurance applications, in simulations, and in some mathematical applications. But APL has been used in a wide variety of contexts and for many and varied purposes.

Standardization

APL has been standardized by the ANSI working group X3J10 and ISO/IEC Joint Technical Committee 1 Subcommittee 22 Working Group 3. The Core APL language is specified in ISO 8485:1989, and the Extended APL language is specified in ISO/IEC 13751:2001.

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
      • It should be noted that the quote above is not literally true. All APL algorithms that shuffle a deck of cards involve the expression 52?52 to generate the numbers from 1 through 52 in random order. That's more than four characters right there, and the question mark symbol does apppear on standard keyboards.
  • "APL is a mistake, carried through to perfection. It is the language of the future for the programming techniques of the past: it creates a new generation of coding bums."
  • "By the time the practical people found out what had happened; APL was so important a part of how IBM ran its business that it could not possibly be uprooted."

Awards

There is an annual award for contributions to APL, the Iverson Award named after the language's creator.

See also

References

Articles