APL syntax: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>")
Tags: Mobile edit Mobile web edit
Line 19: Line 19:


Additionally, there are some syntactic elements that cannot be used as values:
Additionally, there are some syntactic elements that cannot be used as values:
* The assignment arrow <source lang=apl inline>←</source>
* The assignment arrow <source lang=apl inline>←</syntaxhighlight>
* Square brackets <source lang=apl inline>[]</source> used for [[bracket indexing]] and [[function axis]] specification
* Square brackets <source lang=apl inline>[]</syntaxhighlight> used for [[bracket indexing]] and [[function axis]] specification
* Parentheses <source lang=apl inline>()</source> for grouping
* Parentheses <source lang=apl inline>()</syntaxhighlight> for grouping
* The Diamond <source lang=apl inline>⋄</source> and newline characters used as a [[statement separator]]
* The Diamond <source lang=apl inline>⋄</syntaxhighlight> and newline characters used as a [[statement separator]]
* The [[Del]] character <source lang=apl inline>∇</source> used for [[tradfn]]s, or curly braces for [[dfn]]s
* The [[Del]] character <source lang=apl inline>∇</syntaxhighlight> used for [[tradfn]]s, or curly braces for [[dfn]]s
* [[Keyword]]s for [[control structure]]s
* [[Keyword]]s for [[control structure]]s
* The [[branch]] arrow <source lang=apl inline>→</source>
* The [[branch]] arrow <source lang=apl inline>→</syntaxhighlight>


The second group, consisting of fixed syntax written with particular tokens, is common to many programming languages (in fact, APL tends to have a simpler fixed syntax than many contemporary languages). However, the first group is unusual because it means that a variable's syntactic properties are determined by the variable's value and not just by how it's written. This property makes it impossible to parse an APL statement with variables in general: for example, the statement <source lang=apl inline>a b c</source> could be a function application, two function applications, a function modified by an operator, and so on.
The second group, consisting of fixed syntax written with particular tokens, is common to many programming languages (in fact, APL tends to have a simpler fixed syntax than many contemporary languages). However, the first group is unusual because it means that a variable's syntactic properties are determined by the variable's value and not just by how it's written. This property makes it impossible to parse an APL statement with variables in general: for example, the statement <source lang=apl inline>a b c</syntaxhighlight> could be a function application, two function applications, a function modified by an operator, and so on.


== Example array definitions ==
== Example array definitions ==
Line 33: Line 33:
       simplenumvec←1 2 3 4 ⍝ A simple numeric vector
       simplenumvec←1 2 3 4 ⍝ A simple numeric vector
       simplecharvec←'ABCD' ⍝ A simple character vector
       simplecharvec←'ABCD' ⍝ A simple character vector
</source>
</syntaxhighlight>


== Example function definition ==
== Example function definition ==
Line 41: Line 41:
[2]    r←l r                     
[2]    r←l r                     
     ∇   
     ∇   
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]]}}
{{Works in|[[Dyalog APL]], [[APL2]], [[GNU APL]], [[NARS2000]]}}


Line 49: Line 49:
[1]    r←(PreProc larg)Main(PreProc rarg)                                             
[1]    r←(PreProc larg)Main(PreProc rarg)                                             
     ∇   
     ∇   
</source>
</syntaxhighlight>
== Example function application ==
== Example function application ==
<source lang=apl>
<source lang=apl>
Line 60: Line 60:
       2+/1 2 3 4 ⍝ Infix primitive derived function
       2+/1 2 3 4 ⍝ Infix primitive derived function
3 5 7
3 5 7
</source>
</syntaxhighlight>


== Scoping rules ==
== Scoping rules ==

Revision as of 21:08, 10 September 2022

APL's syntax refers to the way programs are written—the arrangement of functions and arrays, along with other values, keywords, and punctuation—in contrast to its semantics, that is, the meaning of the actual operations performed. In part because of APL's close relationship with mathematical notation, its syntax can be very different from typical programming languages. Distinctive features of APL include its use of infix functions, evaluated right to left with no relative precedence ordering, and its division of arrays, functions, and operators into different syntactic as well as semantic classes.

Overview

APL's core syntactic principles are:

  • Arrays as first class citizens
  • Functions act on arrays and have long right scope ("right to left")
  • Operators act on functions or arrays and have long left scope ("left to right")

A typical APL statement consists of an arrangement of the three types above, parentheses for grouping, and possibly assignment. While such individual statements may be enough for simple programming in a session, larger programs are constructed by defining functions, with control structures used to organize APL statements.

Syntactic elements

In most APLs, values in the language can have one of the following classes:

Each of these values can usually be stored in a variable, either by assignment or function definition. Values of these types can also be written directly in many cases, with strings, numeric literals, or array notation, as predefined primitive functions and operators, or as inline dfns. There are some anomalies which do not fit easily into this system, such as Outer Product, which is written with two glyphs, and function-operator overloading.

Additionally, there are some syntactic elements that cannot be used as values:

  • The assignment arrow <source lang=apl inline>←</syntaxhighlight>
  • Square brackets <source lang=apl inline>[]</syntaxhighlight> used for bracket indexing and function axis specification
  • Parentheses <source lang=apl inline>()</syntaxhighlight> for grouping
  • The Diamond <source lang=apl inline>⋄</syntaxhighlight> and newline characters used as a statement separator
  • The Del character <source lang=apl inline>∇</syntaxhighlight> used for tradfns, or curly braces for dfns
  • Keywords for control structures
  • The branch arrow <source lang=apl inline>→</syntaxhighlight>

The second group, consisting of fixed syntax written with particular tokens, is common to many programming languages (in fact, APL tends to have a simpler fixed syntax than many contemporary languages). However, the first group is unusual because it means that a variable's syntactic properties are determined by the variable's value and not just by how it's written. This property makes it impossible to parse an APL statement with variables in general: for example, the statement <source lang=apl inline>a b c</syntaxhighlight> could be a function application, two function applications, a function modified by an operator, and so on.

Example array definitions

<source lang=apl>

     simplenumvec←1 2 3 4 ⍝ A simple numeric vector
     simplecharvec←'ABCD' ⍝ A simple character vector

</syntaxhighlight>

Example function definition

<source lang=apl>

   ∇  r←l Tradfn r              

[1] ⍝ An infix (dyadic) tradfn [2] r←l r

</syntaxhighlight>

Example operator definition

<source lang=apl>

    ∇ r←larg(Main OVER PreProc)rarg                                                 

[1] r←(PreProc larg)Main(PreProc rarg)

</syntaxhighlight>

Example function application

<source lang=apl>

     ÷3         ⍝ Prefix primitive function

0.3333333333

     1+2        ⍝ Infix primitive function

3

     +/1 2 3 4  ⍝ Prefix primitive derived function

10

     2+/1 2 3 4 ⍝ Infix primitive derived function

3 5 7 </syntaxhighlight>

Scoping rules

Functions

Operators

External links


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