Assignment: Difference between revisions
mNo edit summary |
m (Fix infobox and category: syntax, not primitive) |
||
(5 intermediate revisions by 2 users not shown) | |||
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 | {{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 function]]s, 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=== | ||
Common examples (boxing on, and [[index origin]] is 0): | Common examples (boxing on, and [[index origin]] is 0): | ||
< | <syntaxhighlight lang=apl> | ||
⎕←mat←(1 2 3)(1 2 3) | |||
┌─────┬─────┐ | ┌─────┬─────┐ | ||
│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: | ||
< | <syntaxhighlight lang=apl> | ||
mat[0]←1 | |||
mat | |||
┌─┬─────┐ | ┌─┬─────┐ | ||
│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> | ||
mat←3 3⍴⍳9 | |||
mat | |||
0 1 2 | 0 1 2 | ||
3 4 5 | 3 4 5 | ||
6 7 8 | 6 7 8 | ||
mat[0 1;] | |||
0 1 2 | 0 1 2 | ||
3 4 5 | 3 4 5 | ||
mat[0 1;0 1]←0 | |||
mat | |||
0 0 2 | 0 0 2 | ||
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=== | ||
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: | ||
< | <syntaxhighlight lang=apl> | ||
var←42 | |||
var+←1 | |||
var | |||
43 | 43 | ||
</ | </syntaxhighlight> | ||
< | <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: | ||
< | <syntaxhighlight lang=apl> | ||
mat←3 3⍴0 | |||
mat | |||
0 0 0 | 0 0 0 | ||
0 0 0 | 0 0 0 | ||
0 0 0 | 0 0 0 | ||
mat[0 1;1]+←1 | |||
mat | |||
0 1 0 | 0 1 0 | ||
0 1 0 | 0 1 0 | ||
0 0 0 | 0 0 0 | ||
mat[1;1],←'x' | |||
mat | |||
0 1 0 | 0 1 0 | ||
0 x 0 | 0 x 0 | ||
0 0 0 | 0 0 0 | ||
</ | </syntaxhighlight> | ||
== Function assignment == | |||
In modern dialects, the right hand side of assignment may be a [[function]] (or possibly an [[operator]]). This allows [[Anonymous function|anonymous]] and [[tacit]] functions to be named. | |||
<syntaxhighlight lang=apl> | |||
sum←+/ | |||
sum ⍳10 | |||
55 | |||
</syntaxhighlight> | |||
Before the 1980s, only arrays could be assigned: [[defined function]]s included the function name in the header in order to associate it with the function, and [[derived function]]s could not be named directly. Instead the programmer would define a function that explicitly called that derived function on its arguments. [[Iverson]]'s 1978 paper [[Operators and Functions]] used a new [[glyph]], an assignment arrow with a bar above (that is, [[overstruck]] with a [[high minus]]), as [[Adin Falkoff]] objected to the use of the unmodified assignment arrow.<ref>[[Roger Hui]] and [[Morten Kromberg]]. [https://dl.acm.org/doi/abs/10.1145/3386319 ''APL since 1978'']. ACM HOPL IV. 2020-06.</ref> However, Iverson used the assignment arrow directly in several papers he co-authored beginning in 1980.<ref>[[Bob Bernecky]] and [[Ken Iverson]]. [https://www.jsoftware.com/papers/opea.htm Operators and Enclosed Arrays] at [[IPSA '80]]</ref><ref>[[Ken Iverson]] and Peter Wooster. [https://dl.acm.org/doi/abs/10.1145/390007.805349 A function definition operator] at [[APL81]].</ref><ref>[[Ken Iverson]] and [[Arthur Whitney]]. [https://doi.org/10.1145/800071.802236 "Practical uses of a model of APL"] ([https://www.jsoftware.com/papers/APLModel.htm web]) at [[APL82]].</ref> Function assignment was added to [[Dyalog APL]] in version 4.0 in 1986. It was a key feature of [[J]], which supports only anonymous and tacit functions. | |||
== External Links == | == External Links == | ||
Line 70: | Line 82: | ||
* [https://microapl.com/apl_help/ch_020_010_110.htm APLX] | * [https://microapl.com/apl_help/ch_020_010_110.htm APLX] | ||
* J: [https://code.jsoftware.com/wiki/Vocabulary/eqdot local], [https://code.jsoftware.com/wiki/Vocabulary/eqco global] | * J: [https://code.jsoftware.com/wiki/Vocabulary/eqdot local], [https://code.jsoftware.com/wiki/Vocabulary/eqco global] | ||
{{APL | |||
== References == | |||
<references/> | |||
{{APL syntax}}[[Category:APL syntax]] |
Latest revision as of 01:54, 18 October 2024
←
|
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
Function assignment
In modern dialects, the right hand side of assignment may be a function (or possibly an operator). This allows anonymous and tacit functions to be named.
sum←+/ sum ⍳10 55
Before the 1980s, only arrays could be assigned: defined functions included the function name in the header in order to associate it with the function, and derived functions could not be named directly. Instead the programmer would define a function that explicitly called that derived function on its arguments. Iverson's 1978 paper Operators and Functions used a new glyph, an assignment arrow with a bar above (that is, overstruck with a high minus), as Adin Falkoff objected to the use of the unmodified assignment arrow.[1] However, Iverson used the assignment arrow directly in several papers he co-authored beginning in 1980.[2][3][4] Function assignment was added to Dyalog APL in version 4.0 in 1986. It was a key feature of J, which supports only anonymous and tacit functions.
External Links
Documentation
References
- ↑ Roger Hui and Morten Kromberg. APL since 1978. ACM HOPL IV. 2020-06.
- ↑ Bob Bernecky and Ken Iverson. Operators and Enclosed Arrays at IPSA '80
- ↑ Ken Iverson and Peter Wooster. A function definition operator at APL81.
- ↑ Ken Iverson and Arthur Whitney. "Practical uses of a model of APL" (web) at APL82.