---
title: Assignment
description: Naming values, changing parts of arrays, and destructuring
---
[`←`](https://bpl.fast.ai//glyphs/left-arrow.html) assigns the value to its right to the target, the [run](terms.md#term-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 [`←`](https://bpl.fast.ai//glyphs/left-arrow.html), as in `x+←1`, the assignment is modified. For the exact rules, see [Assign](glyphs/left-arrow.md) and [Syntax](syntax.md#assignment).

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Plain assignment

`x←Y` binds `x` to `Y` and returns `Y` without displaying it:

```bpl
x←3
x+2
```

```output
5
```

A value can be a function, such as `+/`:

```bpl
sum←+/
sum 1 2 3
```

```output
6
```

Assignments can be chained:

```bpl
a←b←3
a+b
```

```output
6
```

Put independent assignments on separate lines, or put [`◇`](https://bpl.fast.ai//glyphs/diamond.html) between them, because [`←`](https://bpl.fast.ai//glyphs/left-arrow.html) takes everything to its right:

```bpl
p←1 q←2
p
```

```output
1 2
```

`_←Y` evaluates `Y` and discards its value.

## Modified assignment

`x f←Y` sets `x` to `x f Y`:

```bpl
x+←3
x
```

```output
6
```

Any function works, such as [`,`](https://bpl.fast.ai//glyphs/comma.html) to append:

```bpl
s←"abc"
s,←"d"
s
```

```output
"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`:

```bpl
f←×
x⁝f←4
x
```

```output
24
```

With nothing after the arrow, modified assignment applies the function to the target alone. `x⌽←` sets `x` to `⌽x`:

```bpl
x←1 2 3
x⌽←
x
```

```output
3 2 1
```

## Assigning to part of an array

Assigning to a [subscript](scripts.md#subscripts) target replaces the item at one literal position:

```bpl
v←1 2 3
v₁←9
v
```

```output
1 9 3
```

Use dot brackets to select any positions, in plain or modified assignment:

```bpl
v.[[0 2]]+←10
v
```

```output
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](glyphs/at.md):

```bpl
(2↑v)←8 9
v
```

```output
8 9 13
```

A dfn that returns items of its argument can be the selection too. A space before [`←`](https://bpl.fast.ai//glyphs/left-arrow.html) makes the whole run the target:

```bpl
{ω.[⍸ω>8]}v ←0
v
```

```output
8 0 0
```

The selection can apply a function with rank. `0⌷¹` selects the first item of each row:

```bpl
M←[1 2
   3 4]
(0⌷¹ M)←9
M
```

```output
9 2
9 4
```

Assign to a dot path to write a field of a record:

```bpl
T←["n":1]
T.n+←1
T.n
```

```output
2
```

A plain assignment to a dot path creates any missing records along the path:

```bpl
T.addr.city←"SF"
T
```

```output
["n":2 "addr":["city":"SF"]]
```

A subscript can follow a dot path:

```bpl
T.a←1 2
T.a₁←7
T.a
```

```output
1 7
```

## Inverses and derivatives

`f⁻←g` attaches `g` as the inverse of the function named by `f`. `f⁻`, negative [powers](glyphs/star-diaeresis.md) and [Under](glyphs/at.md) then call `g`:

```bpl
inc←1+
inc⁻←¯1+
inc⍣¯2⁝5
```

```output
3
```

`f∂←g` attaches `g` as the derivative of `f`, which [`∂`](glyphs/partial.md) and [`⇄`](glyphs/right-left-arrows.md) 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`. [`∂`](https://bpl.fast.ai//glyphs/partial.html) calls `g` wherever it differentiates a call of `f`:

```bpl
sq←{ω×ω}
sq∂←{α×2×ω}
sq∂3
```

```output
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:

```bpl
sq⁻←√
[sq 3;sq∂ 3;sq⁻ 9]
```

```output
9 6 3
```

## Destructuring

A list of names in brackets destructures a value by position:

```bpl
[a b]←10 20
a+b
```

```output
30
```

Each name is assigned one major cell of a higher-rank value, such as one row of a matrix:

```bpl
[top bottom]←2 3ρι6ₓ
bottom
```

```output
[3 4 5]ₓ
```

A single value goes to every name:

```bpl
[c d]←5
c d
```

```output
5 5
```

A nested list of names destructures a nested value:

```bpl
[e [g h]]←[1 [2 3]]
h
```

```output
3
```

Modified assignment works on a list of names too:

```bpl
[a b]+←10
a b
```

```output
20 30
```

Inside brackets, every name is a target, even one that holds a function, such as `f`:

```bpl
[f n]←1 2
f
```

```output
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:

```bpl
[b: a:x]←["a":1 "b":2 "c":3]
x b
```

```output
1 2
```

A program takes names from a [module](modules.md) this way, as in `[disp:]←•load "lib/array.bpl"`.

## Names inside dfns

Inside a dfn, plain assignment makes a local name:

```bpl
total←0
set←{total←ω ◇ total}
_←set 5
total
```

```output
0
```

Modified and selective assignment, including assigning an inverse or derivative, update the nearest existing binding, even outside the dfn:

```bpl
add←{total+←ω ◇ total}
_←add 5
total
```

```output
5
```

Arrays have value semantics, as numbers do:

```bpl
u←1 2
w←u
u₀←9
w
```

```output
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.
