Assignment
Naming values, changing parts of arrays, and destructuring
← assigns the value to its right to the target, the run just before it. Besides plain names, a target can be a selection, a dot path, a subscript or a list of names. With a function just before ←, as in x+←1, the assignment is modified. For the exact rules, see Assign and Syntax.
Plain assignment
x←Y binds x to Y and returns Y without displaying it:
x←3
x+2
5
A value can be a function, such as +/:
sum←+/
sum 1 2 3
6
Assignments can be chained:
a←b←3
a+b
6
Put independent assignments on separate lines, or put ◇ between them, because ← takes everything to its right:
p←1 q←2
p
1 2
_←Y evaluates Y and discards its value.
Modified assignment
x f←Y sets x to x f Y:
x+←3
x
6
Any function works, such as , to append:
s←"abc"
s,←"d"
s
"abcd"
To modify with a named function, write x⁝f←Y, which means x←x f Y. In x f←Y, the target would be f:
f←×
x⁝f←4
x
24
With nothing after the arrow, modified assignment applies the function to the target alone. x⌽← sets x to ⌽x:
x←1 2 3
x⌽←
x
3 2 1
Assigning to part of an array
Assigning to a subscript target replaces the item at one literal position:
v←1 2 3
v₁←9
v
1 9 3
Use dot brackets to select any positions, in plain or modified assignment:
v.[[0 2]]+←10
v
11 9 13
For any other selection, put the target in parentheses and use a function that only selects or rearranges, as for structural Under:
(2↑v)←8 9
v
8 9 13
A dfn that returns items of its argument can be the selection too. A space before ← makes the whole run the target:
{ω.[⍸ω>8]}v ←0
v
8 0 0
The selection can apply a function with rank. 0⌷¹ selects the first item of each row:
M←[1 2
3 4]
(0⌷¹ M)←9
M
9 2
9 4
Assign to a dot path to write a field of a record:
T←["n":1]
T.n+←1
T.n
2
A plain assignment to a dot path creates any missing records along the path:
T.addr.city←"SF"
T
["n":2 "addr":["city":"SF"]]
A subscript can follow a dot path:
T.a←1 2
T.a₁←7
T.a
1 7
Inverses and derivatives
f⁻←g attaches g as the inverse of the function named by f. f⁻, negative powers and Under then call g:
inc←1+
inc⁻←¯1+
inc⍣¯2⁝5
3
f∂←g attaches g as the derivative of f, which ∂ and ⇄ call. g is a JVP rule: t g x gives the derivative of f at x along the direction t, and must be linear in t. ∂ calls g wherever it differentiates a call of f:
sq←{ω×ω}
sq∂←{α×2×ω}
sq∂3
6
Calling the function still runs its original definition. Assigning an inverse does not change its derivative rule, and assigning a derivative does not change its inverse:
sq⁻←√
[sq 3;sq∂ 3;sq⁻ 9]
9 6 3
Destructuring
A list of names in brackets destructures a value by position:
[a b]←10 20
a+b
30
Each name is assigned one major cell of a higher-rank value, such as one row of a matrix:
[top bottom]←2 3ρι6ₓ
bottom
[3 4 5]ₓ
A single value goes to every name:
[c d]←5
c d
5 5
A nested list of names destructures a nested value:
[e [g h]]←[1 [2 3]]
h
3
Modified assignment works on a list of names too:
[a b]+←10
a b
20 30
Inside brackets, every name is a target, even one that holds a function, such as f:
[f n]←1 2
f
1
A list of names destructures by position even when the value has keys. A pattern destructures by key instead. Each cell of a pattern is a key, then the name that is assigned the key's item, as in [a:x]. A cell with only a key, as in [a:], assigns the item to the name spelled like the key. A key is never a value: the a in [a:x] stands for the key "a", whatever a holds. Use a string key, as in ["first name":n], when the key isn't a valid name. The cells can come in any order:
[b: a:x]←["a":1 "b":2 "c":3]
x b
1 2
A program takes names from a module this way, as in [disp:]←•load "lib/array.bpl".
Names inside dfns
Inside a dfn, plain assignment makes a local name:
total←0
set←{total←ω ◇ total}
_←set 5
total
0
Modified and selective assignment, including assigning an inverse or derivative, update the nearest existing binding, even outside the dfn:
add←{total+←ω ◇ total}
_←add 5
total
5
Arrays have value semantics, as numbers do:
u←1 2
w←u
u₀←9
w
1 2
An array is copied only when it is modified while another name or array holds it. Above, u₀←9 copies u first, because w holds it too. Assigning to part of an array that nothing else holds changes the array in place. x,←v and x⍪←v add to the end of the array's storage. x←x,v always copies, because x still holds the old array while x,v is computed.