Assignment: Difference between revisions

Jump to navigation Jump to search
72 bytes added ,  21:01, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
(APL uses 6-space indents)
m (Text replacement - "<source" to "<syntaxhighlight")
Line 1: Line 1:
{{Built-in|Assignment|←}} allows associating a name with an [[array]] value. Some dialects also allow assignment of function and operator values using the assignment arrow. In [[defined functions]], assignment is global by default, but can be made local through explicit mention of the target name in the function header, or through dynamic [[shadow]]ing using <source lang=apl inline>⎕SHADOW</source>. In [[dfn]]s, assignments are local by default, but can be made global by explicit mention of the target namespace. Modified/indexed/selective assignment updates the most local definition.
{{Built-in|Assignment|←}} allows associating a name with an [[array]] value. Some dialects also allow assignment of function and operator values using the assignment arrow. In [[defined functions]], assignment is global by default, but can be made local through explicit mention of the target name in the function header, or through dynamic [[shadow]]ing using <syntaxhighlight lang=apl inline>⎕SHADOW</source>. In [[dfn]]s, assignments are local by default, but can be made global by explicit mention of the target namespace. Modified/indexed/selective assignment updates the most local definition.
==Examples==
==Examples==
===Basic usage===
===Basic usage===
Common examples (boxing on, and [[index origin]] is 0):
Common examples (boxing on, and [[index origin]] is 0):
<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕←mat←(1 2 3)(1 2 3)
       ⎕←mat←(1 2 3)(1 2 3)
┌─────┬─────┐
┌─────┬─────┐
Line 11: Line 11:
===Indexed assignment===
===Indexed assignment===
Individual elements can be updated using index assignment:
Individual elements can be updated using index assignment:
<source lang=apl>
<syntaxhighlight lang=apl>
       mat[0]←1
       mat[0]←1
       mat
       mat
Line 19: Line 19:
</source>
</source>
A semicolon is necessary when dealing with a [[matrix]]:
A semicolon is necessary when dealing with a [[matrix]]:
<source lang=apl>
<syntaxhighlight lang=apl>
       mat←3 3⍴⍳9
       mat←3 3⍴⍳9
       mat
       mat
Line 37: Line 37:
===Modified assignment===
===Modified assignment===
Some dialects allow placing a function the the immediate left of the assignment arrow:
Some dialects allow placing a function the the immediate left of the assignment arrow:
<source lang=apl>
<syntaxhighlight lang=apl>
       var←42
       var←42
       var+←1
       var+←1
Line 43: Line 43:
43
43
</source>
</source>
<source lang=apl inline>var+←1</source> is essentially equivalent to <source lang=apl inline>1⊣var←var+1</source> except that the result is [[shy]].
<syntaxhighlight lang=apl inline>var+←1</source> is essentially equivalent to <syntaxhighlight lang=apl inline>1⊣var←var+1</source> except that the result is [[shy]].
===Modified indexed assignment===
===Modified indexed assignment===
Modified assignment can also be combined with indexed assignment:
Modified assignment can also be combined with indexed assignment:
<source lang=apl>
<syntaxhighlight lang=apl>
       mat←3 3⍴0
       mat←3 3⍴0
       mat
       mat

Navigation menu