Control structure

Some modern versions of APL include a set of keywords for controlling flow of execution. Such keywords allow programmers to create control structures which are commonly used in procedural programming languages.

Example
Dyalog APL has a rich set of flow control keywords, including,  ,  ,   (with the supplementary control words   and  ),  ,  ,  ,   and. The use of control structures defined by these keywords is only allowed in defined functions.

For example, a function implementing the Trabb Pardo–Knuth algorithm using control structures:

History
Relative to other programming languages, which began to adapt structured programming in the late 1950s and almost all supported it by the end of the 1980s, APL was slow to adopt the paradigm. Adoption by mainstream array languages began in the 1990s and some dialects, such as APL2 and GNU APL, have never added support for control structures. Many dialects developed in the 2010s and later also do not include control structures, favoring lightweight dfn syntax in combination with operators such as Each.

Edsger Dijkstra's letter Go To Statement Considered Harmful, published in 1968, is widely considered a turning point in the push for structured programming. At the time, APL programs were written as defined functions using Branch for control flow, but programmers began to publish responses considering alternatives in the mid-1970s. Several programmers showed how to use structured programming in current APL systems, or modify these systems to better support it. Others proposed more radical changes that would allow the user to define control structures. Experiments to extend APL with ALGOL-style control structures also began in the 1970s. APLGOL, first described in 1972, used names beginning with an underlined character, and was supported by a compiler implemented in and targetting APL\360. A similar project called SAPL (for "Structured APL") used reserved words for control structures and was published in 1978.

Control structures, using reserved words, were included in A+ by 1989. In 1994 they were added to both J release 2.0.1, with a dotted syntax such as, and to APL*PLUS III, using the colon-prefixed syntax   that has since been widely adopted in APL. Dyalog introduced similar structures in version 8.0, in 1996. They also appear in SAX, APLX, and NARS2000.

Newer APL dialects such as ngn/apl, APL\iv, dzaima/APL often discard control structures, as well as Branch, on the grounds that dfns along with operators like Each and Reduce or recursion allow similar functionality. Such a possibility was not available in the 1970s because APLs at the time did not allow arbitrary functions to be operands, and was not generally considered before the rise of dfns and tacit programming in the 1990s. April does not support control structures on the grounds that April code should be embedded in Common Lisp, which can handle control flow better. BQN also does not include pre-defined control structures, but its first-class functions and list literals allow a similar syntax to be achieved without extending the language.

Tutorials

 * APL Cultivation: Control structures
 * Bernard Legrand. Mastering Dyalog APL (page 189). Dyalog Ltd. November 2009.

Documentation

 * Dyalog
 * APLX