Assignment: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (fixed indentation)
No edit summary
Line 1: Line 1:
You can assign a value to a variable with the glyph: ''.
{{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.
==Examples==
===Basic usage===
Common examples (boxing on, and [[index origin]] is 0):
<source lang=apl>
  ⎕←mat←(1 2 3)(1 2 3)
┌─────┬─────┐
│1 2 3│1 2 3│
└─────┴─────┘
</source>
===Indexed assignment===
Individual elements can be updated using index assignment:
<source lang=apl>
  mat[0]←1
  mat
┌─┬─────┐
│1│1 2 3│
└─┴─────┘
<source>
A semicolon is necessary when dealing with a [[matrix]]:
<source lang=apl>
  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
</source>
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:
<source lang=apl>
  var←42
  var+←1
  var
43
</source>
<source>var+←1</source> is essentially equivalent to <source>1⊣var←var+1</source> except that the result is [[shy]].
===Modified indexed assignment===
Modified assignment can also be combined with indexed assignment:
<source lang=apl>
  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
</source>
== External Links ==


Common examples (boxing on, and ⎕io is 0):
=== Documentation ===
    mat←(1 2 3)(1 2 3)
 
    mat
* [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Assignment.htm Dyalog]
┌─────┬─────┐
* [https://microapl.com/apl_help/ch_020_010_110.htm APLX]
│1 2 3│1 2 3│
* J: [https://code.jsoftware.com/wiki/Vocabulary/eqdot local], [https://code.jsoftware.com/wiki/Vocabulary/eqco global]
└─────┴─────┘
{{APL built-ins}}[[Category:Primitive functions]]
    mat[0]←1  ⍝ indexed assignment
    mat
┌─┬─────┐
│1│1 2 3│
└─┴─────┘
    mat←3 3⍴⍳9
    mat
0 1 2
3 4 5
6 7 8
    mat[0 1;] ⍝ 1 semicolon is necessary when dealing with 2D arrays, 2 semicolons for 3D arrays etc.
0 1 2
3 4 5
    mat[0 1;0 1]←0
    mat
0 0 2
0 0 5
6 7 8
    ⍝ incrementing (or any dyadic function) parts of an array
    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

Revision as of 01:27, 13 February 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 31 2 3
└─────┴─────┘

Indexed assignment

Individual elements can be updated using index assignment:

   mat[0]1
   mat
┌─┬─────┐
11 2 3
└─┴─────┘
<source>
A semicolon is necessary when dealing with a [[matrix]]:
<source lang=apl>
   mat3 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:

   var42
   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:

   mat3 30
   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

External Links

Documentation

APL built-ins [edit]
Primitive functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentitySelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndex
Selector Index generatorGradeIndex OfInterval IndexIndicesDeal
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Name classCase convertUnicode convert
Operators SearchReplace