Sharpening your APL knife
Basics -3-
Assignment -1-
X←YAssignment allocates the result of the expression Y to the name or names in X.
- ⍝ Note that ← is an ordinary function
- ⎕←X[2]←Y
- ⍝ Prints the result of expression Y to ⎕SE, not X
- ⍝ This is true for all kinds of assignments
- ⍴X←Y
- ⍝ Reports the shape of expression Y to ⎕SE,
- ⍝ not the shape of X
Note that the result of the expression under very special circumstances might be different from what is actually assigned
Assignment -2-
A B C←XMore than one name can be specified. If so, X must be either a scalar or a vector with an appropriate length
- ⍝ Note that the syntax is ambiguous:
- ⍝ "A" might be a monadic function!
- ⍝ The APL2 syntax, not demanded but allowed by Dyalog:
- (A B C)←X
- ⍝ If "A" is a monadic fns this results in a VALUE ERROR
Why is it a VALUE ERROR?!
Selective Assignment
({expression} X)←Y
- X is the name of an existing variable
- EXP is an expression that selects elements of X
- Y is an array expression
- The result of expression Y is allocated to the selected elements of Y
- Not all functions can be used in EXP but:
Selective Assignment: Examples -1-
- X←'Hello, world'
- (¯5↑X)←'universe'
- LENGTH ERROR
- (¯5↑X)←5↑'universe'
- ⎕←X
- Hello, unive
Selective Assignment: Examples -2-
- X←1 2 3 4 5 6
- ((X∊3 4 5)/X)←0
- ⎕←X
- 1 2 0 0 0 6
- (¯5↑X)←5↑'universe'
- ⎕←X
- 1 unive
Selective Assignment: Examples -3-
- X←2 3⍴⍳6
- ((,X∊2 3)/,X)←¯1 ¯2
- ⎕←X
- 1 ¯1 ¯2
- 4 5 6
Selective Assignment: Examples -4-
- X←3 4⍴⍳12
- (2 2⍴X)←0
- ⎕←X
- 0 0 0 0
- 5 6 7 8
- 9 10 11 12
- ⍝ Important is which elements are taken,
⍝ not what will happen to them
Modified Assignment by Example -1-
- i←0
- i+←1 ⍝ increase i by one
- v←1 2 3
- v×←2 ⋄ ⎕←v
- 2 4 6
Modified Assignment by Example -2-
- v←1 2 3 4 5 6 7 8
- v/⍨←~v∊4 5 6
- ⎕←v
- 1 2 3 7 8
- VeryLongName←VeryLongName,0
- VeryLongName,←0
The ⍨ operator is discussed in detail later
Modified Assignment by Example -3-
- v←⍳8
- (v/⍨v>5)←0
- SYNTAX ERROR ⍝ Not supported, I'm afraid
- ((v>5)/v)←0 ⋄ ⎕←v
- 1 2 3 4 5 0 0 0
- ((v>2)/v)×←2 ⋄ ⎕←v
- 1 2 6 8 10 0 0 0
Modified Assignment by Example -4-
- ⎕fx ↑'r←x Add y' 'r←x+y'
- v←1 2 3
- v(Add)←10
- ⎕←v
- 11 12 13
- ⍝ but:
- v Add←10
- ⎕←v
- 21 22 23
End