APL syntax: Difference between revisions

Jump to navigation Jump to search
262 bytes added ,  22:16, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
(There's a Wikipedia page on this)
m (Text replacement - "<source" to "<syntaxhighlight")
 
(5 intermediate revisions by 3 users not shown)
Line 16: Line 16:
* [[Niladic function]]s, which are evaluated immediately to become one of the above values
* [[Niladic function]]s, which are evaluated immediately to become one of the above values
* In some APLs, [[hyperator]]s continue the array-function-operator hierarchy.
* In some APLs, [[hyperator]]s continue the array-function-operator hierarchy.
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 [[string]]s, [[numeric literal]]s, or [[array notation]], as predefined [[primitive]] functions and operators, or as inline [[dfn]]s. There are some anomalies which do not fit easily into this system, such as [[Outer Product]], which is written with two [[glyph]]s, and [[function-operator overloading]].
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 [[string]]s, [[numeric literal]]s, or [[array notation]], as predefined [[primitive function]]s and [[primitive operator|operators]], or as inline [[dfn]]s. There are some anomalies which do not fit easily into this system, such as [[Outer Product]], which is written with two [[glyph]]s, and [[function-operator overloading]].


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 <syntaxhighlight lang=apl inline>←</syntaxhighlight>
* Square brackets <source lang=apl inline>[]</source> used for [[bracket indexing]] and [[function axis]] specification
* Square brackets <syntaxhighlight lang=apl inline>[]</syntaxhighlight> used for [[bracket indexing]] and [[function axis]] specification
* Parentheses <source lang=apl inline>()</source> for grouping
* Parentheses <syntaxhighlight lang=apl inline>()</syntaxhighlight> for grouping
* The diamond and newline characters used as a [[statement separator]]
* The Diamond <syntaxhighlight 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 <syntaxhighlight 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 <syntaxhighlight 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 <syntaxhighlight 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 ==
<source lang=apl>
<syntaxhighlight lang=apl>
       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 ==
<source lang=apl>
<syntaxhighlight lang=apl>
     ∇  r←l Tradfn r               
     ∇  r←l Tradfn r               
[1]    ⍝ An infix (dyadic) tradfn
[1]    ⍝ An infix (dyadic) tradfn
[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]]}}


== Example operator definition ==
== Example operator definition ==
<source lang=apl>
<syntaxhighlight lang=apl>
     ∇ r←larg(Main OVER PreProc)rarg                                                 
     ∇ r←larg(Main OVER PreProc)rarg                                                 
[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>
<syntaxhighlight lang=apl>
       ÷3        ⍝ Prefix primitive function
       ÷3        ⍝ Prefix primitive function
0.3333333333
0.3333333333
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 ==

Navigation menu