Assignment: Difference between revisions
m (Text replacement - "<source" to "<syntaxhighlight") |
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 <syntaxhighlight lang=apl inline>⎕SHADOW</ | {{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</syntaxhighlight>. 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=== | ||
Line 8: | Line 8: | ||
│1 2 3│1 2 3│ | │1 2 3│1 2 3│ | ||
└─────┴─────┘ | └─────┴─────┘ | ||
</ | </syntaxhighlight> | ||
===Indexed assignment=== | ===Indexed assignment=== | ||
Individual elements can be updated using index assignment: | Individual elements can be updated using index assignment: | ||
Line 17: | Line 17: | ||
│1│1 2 3│ | │1│1 2 3│ | ||
└─┴─────┘ | └─┴─────┘ | ||
</ | </syntaxhighlight> | ||
A semicolon is necessary when dealing with a [[matrix]]: | A semicolon is necessary when dealing with a [[matrix]]: | ||
<syntaxhighlight lang=apl> | <syntaxhighlight lang=apl> | ||
Line 33: | Line 33: | ||
0 0 5 | 0 0 5 | ||
6 7 8 | 6 7 8 | ||
</ | </syntaxhighlight> | ||
For higher-[[rank]] arrays, the number of semicolons needed is one less than the array rank. | For higher-[[rank]] arrays, the number of semicolons needed is one less than the array rank. | ||
===Modified assignment=== | ===Modified assignment=== | ||
Line 42: | Line 42: | ||
var | var | ||
43 | 43 | ||
</ | </syntaxhighlight> | ||
<syntaxhighlight lang=apl inline>var+←1</ | <syntaxhighlight lang=apl inline>var+←1</syntaxhighlight> is essentially equivalent to <syntaxhighlight lang=apl inline>1⊣var←var+1</syntaxhighlight> 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: | ||
Line 62: | Line 62: | ||
0 x 0 | 0 x 0 | ||
0 0 0 | 0 0 0 | ||
</ | </syntaxhighlight> | ||
== External Links == | == External Links == | ||
Revision as of 22:12, 10 September 2022
←
|
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 shadowing using ⎕SHADOW
. In dfns, 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
Basic usage
Common examples (boxing on, and index origin is 0):
⎕←mat←(1 2 3)(1 2 3) ┌─────┬─────┐ │1 2 3│1 2 3│ └─────┴─────┘
Indexed assignment
Individual elements can be updated using index assignment:
mat[0]←1 mat ┌─┬─────┐ │1│1 2 3│ └─┴─────┘
A semicolon is necessary when dealing with a matrix:
mat←3 3⍴⍳9 mat 0 1 2 3 4 5 6 7 8 mat[0 1;] 0 1 2 3 4 5 mat[0 1;0 1]←0 mat 0 0 2 0 0 5 6 7 8
For higher-rank arrays, the number of semicolons needed is one less than the array rank.
Modified assignment
Some dialects allow placing a function the the immediate left of the assignment arrow:
var←42 var+←1 var 43
var+←1
is essentially equivalent to 1⊣var←var+1
except that the result is shy.
Modified indexed assignment
Modified assignment can also be combined with indexed assignment:
mat←3 3⍴0 mat 0 0 0 0 0 0 0 0 0 mat[0 1;1]+←1 mat 0 1 0 0 1 0 0 0 0 mat[1;1],←'x' mat 0 1 0 0 x 0 0 0 0