Control structure: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(→‎History: SAX documentation has control words)
m (Text replacement - "first-class function" to "first-class function")
 
(5 intermediate revisions by 2 users not shown)
Line 2: Line 2:


==Example==
==Example==
[[Dyalog APL]] has a rich set of flow control keywords, including <source inline lang=apl>:If</source>, <source inline lang=apl>:While</source>, <source inline lang=apl>:Repeat</source>, <source inline lang=apl>:For</source> (with the supplementary control words <source inline lang=apl>:In</source> and <source inline lang=apl>:InEach</source>), <source inline lang=apl>:Select</source>, <source inline lang=apl>:With</source>, <source inline lang=apl>:Trap</source>, <source inline lang=apl>:Hold</source> and <source inline lang=apl>:Disposable</source>. The use of control structures defined by these keywords is only allowed in [[defined function]]s.
[[Dyalog APL]] has a rich set of flow control keywords, including <syntaxhighlight inline lang=apl>:If</syntaxhighlight>, <syntaxhighlight inline lang=apl>:While</syntaxhighlight>, <syntaxhighlight inline lang=apl>:Repeat</syntaxhighlight>, <syntaxhighlight inline lang=apl>:For</syntaxhighlight> (with the supplementary control words <syntaxhighlight inline lang=apl>:In</syntaxhighlight> and <syntaxhighlight inline lang=apl>:InEach</syntaxhighlight>), <syntaxhighlight inline lang=apl>:Select</syntaxhighlight>, <syntaxhighlight inline lang=apl>:With</syntaxhighlight>, <syntaxhighlight inline lang=apl>:Trap</syntaxhighlight>, <syntaxhighlight inline lang=apl>:Hold</syntaxhighlight> and <syntaxhighlight inline lang=apl>:Disposable</syntaxhighlight>. The use of control structures defined by these keywords is only allowed in [[defined function]]s.


For example, a function implementing the [[wikipedia:TPK algorithm|Trabb Pardo–Knuth algorithm]] using control structures:
For example, a function implementing the [[wikipedia:TPK algorithm|Trabb Pardo–Knuth algorithm]] using control structures:
<source lang=apl>
<syntaxhighlight lang=apl>
∇ {res}←trabb;f;S;i;a;y
∇ {res}←trabb;f;S;i;a;y
   f←{(0.5*⍨|⍵)+5×⍵*3}
   f←{(0.5*⍨|⍵)+5×⍵*3}
Line 17: Line 17:
   :EndFor
   :EndFor
</source>
</syntaxhighlight>


==History==
==History==
Relative to other programming languages, which began to adapt [[wikipedia:Structured programming|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]].
Relative to other programming languages, which began to adapt [[wikipedia:Structured programming|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 [[wikipedia:Go To Statement Considered Harmful|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 function]]s 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.<ref>W. K. Giloi and R. Hoffmann. [https://dl.acm.org/doi/10.1145/800114.803676 Adding a modern control structure to APL without changing the syntax] at [[APL76]].</ref><ref>[[Garth Foster]]. [https://dl.acm.org/doi/10.1145/800136.804483 On the locus of program flow within and among secondary functions] at [[APL79]].</ref><ref>Edward V. Eusebi. [https://dl.acm.org/doi/10.1145/255315.255365 Operators for program control] at [[APL85]].</ref> Others proposed more radical changes that would allow the user to define control structures.<ref>Denis P. Samson. [https://dl.acm.org/doi/10.1145/800058.801110 A proposal for control structures in APL] at [[APL84]].</ref><ref>Robert G. Willhoft. [https://dl.acm.org/doi/10.1145/166198.166235 Structured APL: a proposal for block structured control flow in APL] at [[APL93]].</ref> 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]].<ref>R. A. Kelley. "APLGOL, a Structured Programming Language for APL", IBM Palo Alto Scientific Center Report No. 320-3299, August 1972.</ref> A similar project called SAPL (for "Structured APL") used reserved words for control structures and was published in 1978.<ref>Franklin Kemp. [https://dl.acm.org/doi/10.1145/586040.586043 Design of a structured APL]. [[Quote Quad]] Volume 9 Issue 1. 1978-09.</ref>
[[Edsger Dijkstra]]'s letter [[wikipedia:Go To Statement Considered Harmful|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 function]]s using [[Branch]] for control flow, but programmers began to publish responses considering alternatives in the mid-1970s (although [[Phil Abrams]] had suggested control structures based on ALGOL 60 in relation to [[IVSYS/7090]] in 1966<ref>[[Philip Abrams]]. [http://infolab.stanford.edu/TR/CS-TR-66-47.html An interpreter for Iverson notation]. 1966-08-17. p. 38–39.</ref>). Several programmers showed how to use structured programming in current APL systems, or modify these systems to better support it.<ref>W. K. Giloi and R. Hoffmann. [https://dl.acm.org/doi/10.1145/800114.803676 Adding a modern control structure to APL without changing the syntax] at [[APL76]].</ref><ref>[[Garth Foster]]. [https://dl.acm.org/doi/10.1145/800136.804483 On the locus of program flow within and among secondary functions] at [[APL79]].</ref><ref>Edward V. Eusebi. [https://dl.acm.org/doi/10.1145/255315.255365 Operators for program control] at [[APL85]].</ref> Others proposed more radical changes that would allow the user to define control structures.<ref>Denis P. Samson. [https://dl.acm.org/doi/10.1145/800058.801110 A proposal for control structures in APL] at [[APL84]].</ref><ref>Robert G. Willhoft. [https://dl.acm.org/doi/10.1145/166198.166235 Structured APL: a proposal for block structured control flow in APL] at [[APL93]].</ref> 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]].<ref>R. A. Kelley. "APLGOL, a Structured Programming Language for APL", IBM Palo Alto Scientific Center Report No. 320-3299, August 1972.</ref> A similar project called SAPL (for "Structured APL") used reserved words for control structures and was published in 1978.<ref>Franklin Kemp. [https://dl.acm.org/doi/10.1145/586040.586043 Design of a structured APL]. [[Quote Quad]] Volume 9 Issue 1. 1978-09.</ref>


Control structures, using reserved words, were included in [[A+]] by 1989.<ref name="HOPL">[[Roger Hui]] and [[Morten Kromberg]]. [https://dl.acm.org/doi/abs/10.1145/3386319 ''APL since 1978'']. ACM HOPL IV. 2020-06.</ref> [[J]] added control structures such as <source inline lang=j>while.</source> in the 1994 release 2.0.1, and [[Dyalog]] introduced them with colon prefixes in version 8.0, in 1996. Both languages added for-each structures shortly afterwards. [[APL*PLUS]] included control structures by 1995.<ref>[[Robert Bernecky]]. [https://dl.acm.org/doi/10.1145/206913.206949 The role of dynamic programming & control structures in performance] at [[APL95]].</ref> They also appear in [[SAX]], [[APLX]], and [[NARS2000]].
Control structures, using reserved words, were included in [[A+]] by 1989.<ref name="HOPL">[[Roger Hui]] and [[Morten Kromberg]]. [https://dl.acm.org/doi/abs/10.1145/3386319 ''APL since 1978'']. ACM HOPL IV. 2020-06.</ref> A 1991 proposal for a unified control structure<ref>F. H. D. van Batenburg. [https://dl.acm.org/doi/10.1145/114054.114092 L-one-two-three (L1:..L2:..L3:) considered harmful] at [[APL91]].</ref> inspired an implementation using <syntaxhighlight lang=apl inline></syntaxhighlight> in [[APL2C]], a dialect that was implemented beginning in 1991 but only publicized in 2000. In 1994 keyword control structures were added to both [[J]] release 2.0.1,<ref>Dave Ziemann. [http://archive.vector.org.uk/art10011170 Review: J Release 2]. [[Vector journal]] Volume 11 Issue 1. 1994-07.</ref> with a dotted syntax such as <syntaxhighlight inline lang=j>while.</syntaxhighlight>, and to [[APL*PLUS]] III, using the colon-prefixed syntax <syntaxhighlight inline lang=apl>:While</syntaxhighlight> that has since been widely adopted in APL.<ref>James Wheeler. [http://archive.vector.org.uk/art10010810 Meeting: Presentation of APL*PLUS III]. [[Vector journal]] Volume 11 Issue 1. 1994-07.</ref><ref>[[Adrian Smith]]. [http://archive.vector.org.uk/art10008540 Review: APL*PLUS III Control Structures]. [[Vector journal]] Volume 11 Issue 1. 1994-07.</ref> [[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 [[dfn]]s 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.<ref>[[Marshall Lochbaum]]. [https://mlochbaum.github.io/BQN/doc/control.html Control flow in BQN]. Accessed 2021-03-06.</ref>
Newer APL dialects such as [[ngn/apl]], [[APL\iv]], [[dzaima/APL]] often discard control structures, as well as [[Branch]], on the grounds that [[dfn]]s 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 function]]s and list literals allow a similar syntax to be achieved without extending the language.<ref>[[Marshall Lochbaum]]. [https://mlochbaum.github.io/BQN/doc/control.html Control flow in BQN]. Accessed 2021-03-06.</ref>


==External links==
==External links==

Latest revision as of 02:09, 6 September 2024

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 :If, :While, :Repeat, :For (with the supplementary control words :In and :InEach), :Select, :With, :Trap, :Hold and :Disposable. 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:

∇ {res}←trabb;f;S;i;a;y
  f←{(0.5*⍨|⍵)+5×⍵*3}
  S←,⍎{⍞←⍵ ⋄ (≢⍵)↓⍞}'Please, enter 11 numbers: '
  :For i a :InEach (⌽⍳≢S)(⌽S)
      :If 400<y←f(a)
          ⎕←'Too large: ',⍕i
      :Else
          ⎕←i,y
      :EndIf
  :EndFor
∇

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 (although Phil Abrams had suggested control structures based on ALGOL 60 in relation to IVSYS/7090 in 1966[1]). Several programmers showed how to use structured programming in current APL systems, or modify these systems to better support it.[2][3][4] Others proposed more radical changes that would allow the user to define control structures.[5][6] 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.[7] A similar project called SAPL (for "Structured APL") used reserved words for control structures and was published in 1978.[8]

Control structures, using reserved words, were included in A+ by 1989.[9] A 1991 proposal for a unified control structure[10] inspired an implementation using in APL2C, a dialect that was implemented beginning in 1991 but only publicized in 2000. In 1994 keyword control structures were added to both J release 2.0.1,[11] with a dotted syntax such as while., and to APL*PLUS III, using the colon-prefixed syntax :While that has since been widely adopted in APL.[12][13] 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.[14]

External links

Tutorials

Documentation

References

  1. Philip Abrams. An interpreter for Iverson notation. 1966-08-17. p. 38–39.
  2. W. K. Giloi and R. Hoffmann. Adding a modern control structure to APL without changing the syntax at APL76.
  3. Garth Foster. On the locus of program flow within and among secondary functions at APL79.
  4. Edward V. Eusebi. Operators for program control at APL85.
  5. Denis P. Samson. A proposal for control structures in APL at APL84.
  6. Robert G. Willhoft. Structured APL: a proposal for block structured control flow in APL at APL93.
  7. R. A. Kelley. "APLGOL, a Structured Programming Language for APL", IBM Palo Alto Scientific Center Report No. 320-3299, August 1972.
  8. Franklin Kemp. Design of a structured APL. Quote Quad Volume 9 Issue 1. 1978-09.
  9. Roger Hui and Morten Kromberg. APL since 1978. ACM HOPL IV. 2020-06.
  10. F. H. D. van Batenburg. L-one-two-three (L1:..L2:..L3:) considered harmful at APL91.
  11. Dave Ziemann. Review: J Release 2. Vector journal Volume 11 Issue 1. 1994-07.
  12. James Wheeler. Meeting: Presentation of APL*PLUS III. Vector journal Volume 11 Issue 1. 1994-07.
  13. Adrian Smith. Review: APL*PLUS III Control Structures. Vector journal Volume 11 Issue 1. 1994-07.
  14. Marshall Lochbaum. Control flow in BQN. Accessed 2021-03-06.
APL syntax [edit]
General Comparison with traditional mathematicsPrecedenceTacit programming (Train, Hook, Split composition)
Array Numeric literalStringStrand notationObject literalArray notation (design considerations)
Function ArgumentFunction valenceDerived functionDerived operatorNiladic functionMonadic functionDyadic functionAmbivalent functionDefined function (traditional)DfnFunction train
Operator OperandOperator valenceTradopDopDerived operator
Assignment MultipleIndexedSelectiveModified
Other Function axisBracket indexingBranchStatement separatorQuad nameSystem commandUser commandKeywordDot notationFunction-operator overloadingControl structureComment