1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
<head>
<link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/>
<link href="../style.css" rel="stylesheet"/>
<title>BQN Tutorial: Variables</title>
</head>
<div class="nav">(<a href="https://github.com/mlochbaum/BQN">github</a>) / <a href="../index.html">BQN</a> / <a href="index.html">tutorial</a></div>
<h1 id="tutorial-variables"><a class="header" href="#tutorial-variables">Tutorial: Variables</a></h1>
<p>To take a proud denizen of the eternal cosmos of values, held for a fleeting instant by the course of code, and bind it. Tie it down with a name, failing always to alter its inner nature but allowing context to reform its outer appearance. So labelled, perhaps through the progress of time it will know escape, or else find itself passed through one bond to another, ever tethered. It's a task to be approached only with respect.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=aGV5IOKGkCAiSGkgdGhlcmUiCgpoZXkg4oi+ICIsIFdvcmxkISI=">↗️</a><pre> <span class='Value'>hey</span> <span class='Gets'>←</span> <span class='String'>"Hi there"</span>
<span class='Value'>hey</span> <span class='Function'>∾</span> <span class='String'>", World!"</span>
"Hi there, World!"
</pre>
<p>Like that.</p>
<h2 id="defining-variables"><a class="header" href="#defining-variables">Defining variables</a></h2>
<p>BQN uses the left-pointing arrow <code><span class='Gets'>←</span></code> to define variables, as shown above. Most of the time it's best to use it in a plain way, with just the name and its definition, but it's also possible to define multiple variables using list notation, or to define a variable as part of a larger expression that continues to the left (in terms of precedence, <code><span class='Gets'>←</span></code> behaves like a function, but it isn't one—it's a part of syntax).</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cGnigL9l4oC/dGVuIOKGkCDin6ggz4AsIOKLhjEsIDEwIOKfqQoKdGVuIMOXIHBpCgp0aHJlZSDii4ggdGVuIC0gdGhyZWUg4oaQIDM=">↗️</a><pre> <span class='Value'>pi</span><span class='Ligature'>‿</span><span class='Value'>e</span><span class='Ligature'>‿</span><span class='Value'>ten</span> <span class='Gets'>←</span> <span class='Bracket'>⟨</span> <span class='Number'>π</span><span class='Separator'>,</span> <span class='Function'>⋆</span><span class='Number'>1</span><span class='Separator'>,</span> <span class='Number'>10</span> <span class='Bracket'>⟩</span>
⟨ 3.141592653589793 2.718281828459045 10 ⟩
<span class='Value'>ten</span> <span class='Function'>×</span> <span class='Value'>pi</span>
31.41592653589793
<span class='Value'>three</span> <span class='Function'>⋈</span> <span class='Value'>ten</span> <span class='Function'>-</span> <span class='Value'>three</span> <span class='Gets'>←</span> <span class='Number'>3</span>
⟨ 3 7 ⟩
</pre>
<p>A variable can't be defined twice in the same <em>scope</em>. Later we'll work with functions and other pieces of code that create their own scopes, but for now all you need to know is that all the code in a tutorial runs in the same scope. So <code><span class='Value'>three</span></code> is already defined, and can't be defined again.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cGnigL9l4oC/dGVuIOKGkCDin6ggz4AsIOKLhjEsIDEwIOKfqQp0aHJlZSDii4ggdGVuIC0gdGhyZWUg4oaQIDMKdGhyZWUg4oaQIDQ=">↗️</a><pre> <span class='Value'>three</span> <span class='Gets'>←</span> <span class='Number'>4</span>
<span class='Error'>Error: Redefinition</span>
</pre>
<p>It's a little crazy to call them variables if the definition can never change, right? Doesn't "variable" <em>mean</em> "able to change"? Fortunately, this is one way in which BQN isn't crazy. You can <em>modify</em> a variable's value with the arrow <code><span class='Gets'>↩</span></code> provided it's already been defined. This never does anything to the original value: that value stays the same; it's just (probably) not the value of the modified variable any more.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cGnigL9l4oC/dGVuIOKGkCDin6ggz4AsIOKLhjEsIDEwIOKfqQp0aHJlZSDii4ggdGVuIC0gdGhyZWUg4oaQIDMKdGhyZWUg4oaQIDQKdGhyZWUg4oapIDQKCnRocmVlID0gMyAgICMgV2FpdCB3aHkgZGlkIEkgZG8gdGhhdAoKMyA9IHRocmVlIOKGqSAzCgpmb3VyIOKGqSAzICAgICMgZm91ciBpc24ndCBkZWZpbmVkIHlldA==">↗️</a><pre> <span class='Value'>three</span> <span class='Gets'>↩</span> <span class='Number'>4</span>
<span class='Value'>three</span> <span class='Function'>=</span> <span class='Number'>3</span> <span class='Comment'># Wait why did I do that
</span>0
<span class='Number'>3</span> <span class='Function'>=</span> <span class='Value'>three</span> <span class='Gets'>↩</span> <span class='Number'>3</span>
1
<span class='Value'>four</span> <span class='Gets'>↩</span> <span class='Number'>3</span> <span class='Comment'># four isn't defined yet
</span><span class='Error'>Error: Undefined identifier</span>
</pre>
<p>It's an odd distinction to have when your program is just one long sequence of statements, because there's only ever one arrow you can use: it just changes annoyingly after you define the variable for the first time. With multiple scopes this isn't the case: if you start a new scope inside another, then you'll still be able to use variables from the outside scope. Then <code><span class='Gets'>↩</span></code> lets you change the value of one of these variables while <code><span class='Gets'>←</span></code> allows you to define your own. If you're coming from a typical curly-brace language, you'd say that <code><span class='Gets'>←</span></code> both declares and assigns a variable, while <code><span class='Gets'>↩</span></code> only assigns it.</p>
<h2 id="variable-roles"><a class="header" href="#variable-roles">Variable roles</a></h2>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=QlFOIOKGkCAiW21hbnkgcGFnZXMgb2Ygc3BlY2lmaWNhdGlvbl0i">↗️</a><pre> <span class='Function'>BQN</span> <span class='Gets'>←</span> <span class='String'>"[many pages of specification]"</span>
<span class='Error'>Error: Role of the two sides in assignment must match</span>
</pre>
<p>Does BQN not know about capital letters? Does it object to self-reference? Why is "<code><span class='Function'>BQN</span></code>" green? At least there's an error message, and a "role" is something we've heard about before. <em>Assignment</em> means anything written with a leftward arrow—either definition or modification.</p>
<p>I'll first confuse you a little more by pointing out that BQN's variables are case-insensitive, and even underscore-insensitive!</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cGnigL9l4oC/dGVuIOKGkCDin6ggz4AsIOKLhjEsIDEwIOKfqQp0aHJlZSDii4ggdGVuIC0gdGhyZWUg4oaQIDMKdGhyZWUg4oaQIDQKdGhyZWUg4oapIDQKMyA9IHRocmVlIOKGqSAzCnRocmVlCnRockVlClRoUmVFCnRocl9FRQpfX3RocmVlCl9UX0hfUl9FX0Vf">↗️</a><pre> <span class='Value'>three</span>
3
<span class='Value'>thrEe</span>
3
<span class='Function'>ThReE</span>
3
<span class='Value'>thr_EE</span>
3
<span class='Modifier'>__three</span>
3
<span class='Modifier2'>_T_H_R_E_E_</span>
3
</pre>
<p>But the syntax highlighter still seems to care, and you'll get a strange result if you try to apply a function to one of the uppercase spellings:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cGnigL9l4oC/dGVuIOKGkCDin6ggz4AsIOKLhjEsIDEwIOKfqQp0aHJlZSDii4ggdGVuIC0gdGhyZWUg4oaQIDMKdGhyZWUg4oaQIDQKdGhyZWUg4oapIDQKMyA9IHRocmVlIOKGqSAzCi0gVGhyZWUKCi0gX3RocmVl">↗️</a><pre> <span class='Function'>-</span> <span class='Function'>Three</span>
-3{𝔽}
<span class='Function'>-</span> <span class='Modifier'>_three</span>
<span class='Error'>Error: Interpreting non-1-modifier as 1-modifier</span>
</pre>
<p>Now might be a good time to <a href="expression.html#one-or-two-arguments">review</a> the earlier material on roles, experiment, and see if you can puzzle out what's happening here. Or a good time to keep reading until the horrifying distortions these texts inevitably wrap around your existence become apparent, so I'll explain that all these names do represent the same value—they all refer to the same variable—but they have different syntactic roles. Just as the same person might sometimes stand in front of the counter to order a coffee and sometimes stand behind it pouring coffee, the same variable is spelled different ways to indicate what it might be doing right now. There's a spelling for each role:</p>
<table>
<thead>
<tr>
<th>Spelling</th>
<th>Role</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Value'>lowercase</span></code></td>
<td>Subject</td>
<td>Argument or operand</td>
</tr>
<tr>
<td><code><span class='Function'>Uppercase</span></code></td>
<td>Function</td>
<td>Function call or operand</td>
</tr>
<tr>
<td><code><span class='Modifier'>_leftUnderscore</span></code></td>
<td>1-modifier</td>
<td>1-modifier call</td>
</tr>
<tr>
<td><code><span class='Modifier2'>_twoUnderscores_</span></code></td>
<td>2-modifier</td>
<td>2-modifier call</td>
</tr>
</tbody>
</table>
<p>The role only depends on the first character of the name, and the last one if the first one was an underscore. Whether the characters in the middle are uppercase or lowercase doesn't matter. I tend to stick to <code><span class='Value'>camelCase</span></code>, but if you prefer <code><span class='Value'>snake_case</span></code>, or choose depending on your current biome, that's also fine (but "snake" and "camel" aren't interchangeable, they are different animals).</p>
<p>By the way, you can also write numbers with underscores in the middle: they'll be ignored just like in names. This can be useful as a thousands separator, for example.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MV8wMDBfMDAw">↗️</a><pre> <span class='Number'>1_000_000</span>
1000000
</pre>
<h2 id="function-assignment"><a class="header" href="#function-assignment">Function assignment</a></h2>
<p>While you could build up a script by computing values and assigning them names, the main way to use assignment in tacit programming is to give names to functions, not data. For example, we might name the base-2 conversion function from our last tutorial:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=QmFzZTIg4oaQICvin5woMuKKuMOXKcK04oiY4oy9CgpCYXNlMiAx4oC/MOKAvzHigL8wCgpCYXNlMiAiMDEwMTAwMDEiLScwJwoKQCArIEJhc2UywqggJzAnIC3LnCAiMDEwMDAwMTAi4oC/IjAxMDEwMDAxIuKAvyIwMTAwMTExMCI=">↗️</a><pre> <span class='Function'>Base2</span> <span class='Gets'>←</span> <span class='Function'>+</span><span class='Modifier2'>⟜</span><span class='Paren'>(</span><span class='Number'>2</span><span class='Modifier2'>⊸</span><span class='Function'>×</span><span class='Paren'>)</span><span class='Modifier'>´</span><span class='Modifier2'>∘</span><span class='Function'>⌽</span>
<span class='Function'>Base2</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span>
10
<span class='Function'>Base2</span> <span class='String'>"01010001"</span><span class='Function'>-</span><span class='String'>'0'</span>
81
<span class='String'>@</span> <span class='Function'>+</span> <span class='Function'>Base2</span><span class='Modifier'>¨</span> <span class='String'>'0'</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>"01000010"</span><span class='Ligature'>‿</span><span class='String'>"01010001"</span><span class='Ligature'>‿</span><span class='String'>"01001110"</span>
"BQN"
</pre>
<p>This strategy allows us to break down a program into smaller parts. However, you can only name a function in this way, not an expression. We'll explain later how to turn an expression into an explicit function. But one thing remains true regardless of how a function is created: functions are just another kind of BQN value, and giving a function a name uses the ordinary definition arrow <code><span class='Gets'>←</span></code>, not any special syntax.</p>
<p>Even if you define a variable to be a function at first, you're not locked in to that choice. You can modify the variable to have a different value (but remember to change the casing to match the new value's role!). If it's a data value, you'll still be able to call it as a function: it will return itself.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=QmFzZTIg4oaQICvin5woMuKKuMOXKcK04oiY4oy9CkJhc2UyCgpiYXNlMiDihqkgMTYgICAjIENoYW5nZSBpdCB0byBhIG51bWJlcgoKQmFzZTIKCkJhc2UyIDY=">↗️</a><pre> <span class='Function'>Base2</span>
+⟜(2⊸×)´∘⌽
<span class='Value'>base2</span> <span class='Gets'>↩</span> <span class='Number'>16</span> <span class='Comment'># Change it to a number
</span>
<span class='Function'>Base2</span>
16
<span class='Function'>Base2</span> <span class='Number'>6</span>
16
</pre>
<h2 id="modifying-part-of-an-array"><a class="header" href="#modifying-part-of-an-array">Modifying part of an array</a></h2>
<p>You cannot modify part of an array. You can't modify an array: an array that differs a little bit from another array <em>is a different array</em>. And this isn't just a terminology choice: it has real effects on how BQN arrays behave and even which arrays are representable, as we'll discuss later.</p>
<p>But say I have a list, and I want to subtract one from one of the elements. With the understanding that the resulting list is different from the first one, BQN allows this!</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IkJRTiIgICAgICAgICAgICAjIEEgbGlzdCBvZiBjaGFyYWN0ZXJzCgot4p+cMeKMvigy4oq44oqRKSAiQlFOIiAgIyBXYWl0IHdoeSBkaWQgSSBkbyB0aGF0">↗️</a><pre> <span class='String'>"BQN"</span> <span class='Comment'># A list of characters
</span>"BQN"
<span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Number'>1</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>2</span><span class='Modifier2'>⊸</span><span class='Function'>⊑</span><span class='Paren'>)</span> <span class='String'>"BQN"</span> <span class='Comment'># Wait why did I do that
</span>"BQM"
</pre>
<table class='primitives'>
<tr>
<td><span class='Modifier2'>⌾</span></td>
<td><kbd>\K</kbd></td>
<td colspan='2'>Under</td>
</tr>
<tr>
<td><span class='Function'>⊑</span></td>
<td><kbd>\I</kbd></td>
<td>First</td>
<td>Pick</td>
</tr>
</table>
<p>Besides using some primitives we haven't seen here, the notation is a little noisy. In return it's very flexible, in that you can apply any function you want, at a location you can select with a large class of BQN functions.</p>
<svg viewBox='-270 -106 540 212'>
<g font-size='20px' text-anchor='middle' fill='currentColor'>
<rect class='code' stroke-width='1' rx='12' x='-200' y='-96' width='400' height='192'/>
<g stroke-width='3'><path class='purple' style='fill:none' d='M-60 -48l120 0m-15 -8l15 8l-15 8'/></g>
<g stroke-width='2'><path class='yellow' style='fill:none' d='M-60 48l120 0m-15 -8l15 8l-15 8M-100 -31.68l0 63.36m8 -15l-8 15l-8 -15M100 -31.68l0 63.36m8 -15l-8 15l-8 -15'/></g>
<g font-size='18px' font-family='BQN,monospace'>
<text dy='0.31em' x='0' y='-69.12'><tspan class='Function'>-</tspan><tspan class='Modifier2'>⟜</tspan><tspan class='Number'>1</tspan><tspan class='Modifier2'>⌾</tspan><tspan class='Paren'>(</tspan><tspan class='Number'>2</tspan><tspan class='Modifier2'>⊸</tspan><tspan class='Function'>⊑</tspan><tspan class='Paren'>)</tspan></text>
<text dy='0.31em' x='0' y='69.12'><tspan class='Function'>-</tspan><tspan class='Modifier2'>⟜</tspan><tspan class='Number'>1</tspan></text>
<text dy='0.31em' x='-135' y='0'><tspan class='Number'>2</tspan><tspan class='Modifier2'>⊸</tspan><tspan class='Function'>⊑</tspan></text>
<text dy='0.31em' x='135' y='0'><tspan class='Number'>2</tspan><tspan class='Modifier2'>⊸</tspan><tspan class='Function'>⊑</tspan></text>
<g class='string'>
<text dy='0.31em' x='-100' y='-48'>"BQN"</text>
<text dy='0.31em' x='-100' y='48'> 'N' </text>
<text dy='0.31em' x='100' y='-48'>"BQM"</text>
<text dy='0.31em' x='100' y='48'> 'M' </text>
</g>
</g>
</g>
</svg>
<p>So let's break this down. The 2-modifier Under (<code><span class='Modifier2'>⌾</span></code>) has two operands: the left one, <code><span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Number'>1</span></code>, subtracts one, and the right one, <code><span class='Number'>2</span><span class='Modifier2'>⊸</span><span class='Function'>⊑</span></code> uses a function we haven't seen before. It uses the right operand to pick out part of its argument, then the left one acts on that part only, and the entire argument, with the necessary modifications, is returned.</p>
<svg viewBox='-174.7 -34 512 188'>
<g font-family='BQN,monospace' font-size='18px' class='Paren' fill='currentColor'>
<rect class='code' stroke-width='1' rx='10' x='-21.68' y='-24' width='205.96' height='168'/>
<text><tspan class='Function'>-</tspan><tspan class='Modifier2'>⟜</tspan><tspan class='Number'>1</tspan><tspan class='Modifier2'>⌾</tspan><tspan class='Paren'>(</tspan><tspan class='Number'>2</tspan><tspan class='Modifier2'>⊸</tspan><tspan class='Function'>⊑</tspan><tspan class='Paren'>)</tspan> <tspan class='String'>"BQN"</tspan></text>
<path stroke='currentColor' fill='none' stroke-width='1' d='M135.5 2.4V98.4H37.94'/>
<path stroke='currentColor' fill='none' stroke-width='1' d='M81.3 2.4V26.4H70.46'/>
<path stroke='currentColor' fill='none' stroke-width='1' d='M59.62 2.4V26.4H70.46'/>
<path stroke='currentColor' fill='none' stroke-width='1' d='M70.46 26.4V74.4H37.94'/>
<path stroke='currentColor' fill='none' stroke-width='1' d='M27.1 2.4V50.4H16.26'/>
<path stroke='currentColor' fill='none' stroke-width='1' d='M5.42 2.4V50.4H16.26'/>
<path stroke='currentColor' fill='none' stroke-width='1' d='M16.26 50.4V74.4H37.94'/>
<path stroke='currentColor' fill='none' stroke-width='1' d='M37.94 74.4V98.4H37.94'/>
<path stroke='currentColor' fill='none' stroke-width='1' d='M37.94 98.4V122.4H-8.13'/>
<g font-size='15px' text-anchor='middle'>
<g class='codeCover' stroke-width='8' stroke-linejoin='round'>
<text x='135.5' y='103'>"BQN"</text>
<text x='81.3' y='31'>⊑</text>
<text x='70.46' y='31'>⊸</text>
<text x='59.62' y='31'>2</text>
<text x='37.94' y='79'>⌾</text>
<text x='27.1' y='55'>1</text>
<text x='16.26' y='55'>⟜</text>
<text x='5.42' y='55'>-</text>
</g>
<g opacity='0.9'>
<text x='135.5' y='103'><tspan class='String'>"BQN"</tspan></text>
<text x='81.3' y='31'><tspan class='Function'>⊑</tspan></text>
<text x='70.46' y='31'><tspan class='Modifier2'>⊸</tspan></text>
<text x='59.62' y='31'><tspan class='Number'>2</tspan></text>
<text x='37.94' y='79'><tspan class='Modifier2'>⌾</tspan></text>
<text x='27.1' y='55'><tspan class='Number'>1</tspan></text>
<text x='16.26' y='55'><tspan class='Modifier2'>⟜</tspan></text>
<text x='5.42' y='55'><tspan class='Function'>-</tspan></text>
</g>
</g>
</g>
</svg>
<p>Well, the function Pick (<code><span class='Function'>⊑</span></code>) isn't doing anything too special here: the left argument is an index and it picks the element at that index from the right argument (which has to be a list, although there's a more complicated case with a compound left argument that we won't talk about now). Elements of a list are numbered starting at 0. This matches with the Range (<code><span class='Function'>↕</span></code>) function we saw earlier, in that the value of Range's result at a particular index is equal to that index. As an illustration, we can pair up each element of a list with its index by calling Range on the list's length.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KOKGlTMpIOKLiMKoICJCUU4iCgoxIOKKkSAiQlFOIg==">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>↕</span><span class='Number'>3</span><span class='Paren'>)</span> <span class='Function'>⋈</span><span class='Modifier'>¨</span> <span class='String'>"BQN"</span>
⟨ ⟨ 0 'B' ⟩ ⟨ 1 'Q' ⟩ ⟨ 2 'N' ⟩ ⟩
<span class='Number'>1</span> <span class='Function'>⊑</span> <span class='String'>"BQN"</span>
'Q'
</pre>
<p>A sometimes-useful shorthand is that with no left argument, <code><span class='Function'>⊑</span></code> indicates First, the first element of a list. So we can quickly replace the first element of a list. The second two elements don't change here, but they're written differently because unlike a list of characters—a string—a list with numbers and characters doesn't use a special notation.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=OOKMvuKKkSAiQlFOIiAgICAgICAgIyBDaGFuZ2UgdGhlIGZpcnN0IGVsZW1lbnQgdG8gOA==">↗️</a><pre> <span class='Number'>8</span><span class='Modifier2'>⌾</span><span class='Function'>⊑</span> <span class='String'>"BQN"</span> <span class='Comment'># Change the first element to 8
</span>⟨ 8 'Q' 'N' ⟩
</pre>
<p>BQN doesn't have a dedicated syntax such as <code><span class='Value'>list</span><span class='Bracket'>[</span><span class='Value'>index</span><span class='Bracket'>]</span></code> to select from a list, because a function is more consistent with the rest of BQN's notation and can be manipulated more easily. This decision has already been useful to us, because Under's right operand is a function! With a special notation we'd have to first "package" index selection into a function to use it.</p>
<table class='primitives'>
<tr>
<td><span class='Function'>↑</span></td>
<td><kbd>\r</kbd></td>
<td></td>
<td>Take</td>
</tr>
<tr>
<td><span class='Function'>↓</span></td>
<td><kbd>\c</kbd></td>
<td></td>
<td>Drop</td>
</tr>
</table>
<p>What other selection functions can we use for Under's right operand? The only limit standing in our way is our knowledge of BQN primitives, which… frankly is pretty limiting. Let's work on that. Starting with the function Take (<code><span class='Function'>↑</span></code>), which selects the first few elements of a list, or the last few.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oaVNwoKNCDihpEg4oaVNyAgICAgICAgICAgIyBUaGUgZmlyc3QgZm91ciBlbGVtZW50cwoK4oy94oy+KDTiirjihpEpIOKGlTcgICAgICAgIyBBbmQgcmV2ZXJzZSB0aGVtCgrijL3ijL4owq804oq44oaRKSDihpU3ICAgICAgIyBPciByZXZlcnNlIHRoZSBsYXN0IGZvdXI=">↗️</a><pre> <span class='Function'>↕</span><span class='Number'>7</span>
⟨ 0 1 2 3 4 5 6 ⟩
<span class='Number'>4</span> <span class='Function'>↑</span> <span class='Function'>↕</span><span class='Number'>7</span> <span class='Comment'># The first four elements
</span>⟨ 0 1 2 3 ⟩
<span class='Function'>⌽</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>4</span><span class='Modifier2'>⊸</span><span class='Function'>↑</span><span class='Paren'>)</span> <span class='Function'>↕</span><span class='Number'>7</span> <span class='Comment'># And reverse them
</span>⟨ 3 2 1 0 4 5 6 ⟩
<span class='Function'>⌽</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>¯4</span><span class='Modifier2'>⊸</span><span class='Function'>↑</span><span class='Paren'>)</span> <span class='Function'>↕</span><span class='Number'>7</span> <span class='Comment'># Or reverse the last four
</span>⟨ 0 1 2 6 5 4 3 ⟩
</pre>
<p>This function takes from the beginning if the left argument is positive and from the end if the left argument is negative. If the left argument is zero, the result is an empty list, so it doesn't really take from the beginning <em>or</em> the end. Another thing to notice about the left argument here is that it's on the left. Less circularly, these sorts of selection primitives in BQN always take the array to select from on the right and a control value describing how to select on the left. Another example is Rotate (<code><span class='Function'>⌽</span></code>), where the rotation amount goes on the left. It doesn't usually make sense to use Rotate in the right operand to Under, but we can easily rotate, say, the contents of element 1 of a list, or all the elements but the first two.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MuKKuOKMveKMvigx4oq44oqRKSAieHl6IuKAvyJBQkNERSLigL8id3h5eiLigL8ieXoiCgoy4oq44oy94oy+KDLiirjihpMpICJYWWFiY2RlIg==">↗️</a><pre> <span class='Number'>2</span><span class='Modifier2'>⊸</span><span class='Function'>⌽</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>1</span><span class='Modifier2'>⊸</span><span class='Function'>⊑</span><span class='Paren'>)</span> <span class='String'>"xyz"</span><span class='Ligature'>‿</span><span class='String'>"ABCDE"</span><span class='Ligature'>‿</span><span class='String'>"wxyz"</span><span class='Ligature'>‿</span><span class='String'>"yz"</span>
⟨ "xyz" "CDEAB" "wxyz" "yz" ⟩
<span class='Number'>2</span><span class='Modifier2'>⊸</span><span class='Function'>⌽</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>2</span><span class='Modifier2'>⊸</span><span class='Function'>↓</span><span class='Paren'>)</span> <span class='String'>"XYabcde"</span>
"XYcdeab"
</pre>
<p>Drop (<code><span class='Function'>↓</span></code>), on display in the second expression above, is a sort of "opposite" to take: it returns all the elements <em>except</em> the ones that Take would select. So for example <code><span class='Number'>2</span><span class='Modifier2'>⊸</span><span class='Function'>↑</span></code> gets the first two elements but <code><span class='Number'>2</span><span class='Modifier2'>⊸</span><span class='Function'>↓</span></code> removes them. Like Take, a negative left argument works from the end, and a left argument of zero returns the full list, so it doesn't drop from the beginning or the end.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=wq8zIOKGkyAiYWJjZGVmZ2giCgoyIOKGkSA0IOKGkyAiYWJjZGVmZ2gi">↗️</a><pre> <span class='Number'>¯3</span> <span class='Function'>↓</span> <span class='String'>"abcdefgh"</span>
"abcde"
<span class='Number'>2</span> <span class='Function'>↑</span> <span class='Number'>4</span> <span class='Function'>↓</span> <span class='String'>"abcdefgh"</span>
"ef"
</pre>
<p>As you can see, by applying Take and then Drop, we can pick out a slice of a list with a given starting point (4) and length (2). Can we use Under to act on that slice only? Yes!</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KCdBJy0nYScp4oq4KyDijL4gKDIg4oaRIDTiirjihpMpICAiYWJjZGVmZ2gi">↗️</a><pre> <span class='Paren'>(</span><span class='String'>'A'</span><span class='Function'>-</span><span class='String'>'a'</span><span class='Paren'>)</span><span class='Modifier2'>⊸</span><span class='Function'>+</span> <span class='Modifier2'>⌾</span> <span class='Paren'>(</span><span class='Number'>2</span> <span class='Function'>↑</span> <span class='Number'>4</span><span class='Modifier2'>⊸</span><span class='Function'>↓</span><span class='Paren'>)</span> <span class='String'>"abcdefgh"</span>
"abcdEFgh"
</pre>
<p>(Here I've snuck in a train <code><span class='Number'>2</span> <span class='Function'>↑</span> <span class='Number'>4</span><span class='Modifier2'>⊸</span><span class='Function'>↓</span></code> to combine the two functions. As an exercise, you might try to write that function using combinators instead, and as an extra hard exercise you might then ponder why someone would want to add trains to a language).</p>
<h2 id="identity-functions"><a class="header" href="#identity-functions">Identity functions</a></h2>
<table class='primitives'>
<tr>
<td><span class='Function'>⊣</span></td>
<td><kbd>\{</kbd></td>
<td>Identity</td>
<td>Left</td>
</tr>
<tr>
<td><span class='Function'>⊢</span></td>
<td><kbd>\}</kbd></td>
<td>Identity</td>
<td>Right</td>
</tr>
</table>
<p>I'm not going to lie. I'm making this its own section so it looks like I plan ahead when I write these tutorials. The function Right (<code><span class='Function'>⊢</span></code>) always returns its right argument, and Left (<code><span class='Function'>⊣</span></code>) returns its left argument if there is one, and the right argument otherwise.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiICJvbmx5IgoK4oqjICJvbmx5IgoKImxlZnQiIOKKoiAicmlnaHQiCgoibGVmdCIg4oqjICJyaWdodCI=">↗️</a><pre> <span class='Function'>⊢</span> <span class='String'>"only"</span>
"only"
<span class='Function'>⊣</span> <span class='String'>"only"</span>
"only"
<span class='String'>"left"</span> <span class='Function'>⊢</span> <span class='String'>"right"</span>
"right"
<span class='String'>"left"</span> <span class='Function'>⊣</span> <span class='String'>"right"</span>
"left"
</pre>
<p>They are not complicated functions: if you're confused it's because you don't understand why anyone would ever use them. Indeed, it's harder to see why these functions are useful than to see what they do. That is a fact.</p>
<h2 id="modified-assignment"><a class="header" href="#modified-assignment">Modified assignment</a></h2>
<p>Let's revisit our question about modifying an array. As we said, the answer to "how do I modify part of an array?" is simply that you can't, and that the question doesn't make sense. But there's a seemingly similar question with a very different answer: "how do I modify part of a variable whose value is an array?" This is because unlike an array, a variable isn't defined by the value it has, but by the name used to refer to it (and the scope it resides in). Here's how we would modify the variable <code><span class='Value'>a</span></code>:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphCgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQph">↗️</a><pre> <span class='Value'>a</span> <span class='Gets'>←</span> <span class='Number'>4</span> <span class='Comment'># First it's a number
</span> <span class='Value'>a</span>
4
<span class='Value'>a</span> <span class='Gets'>↩</span> <span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>5</span><span class='Ligature'>‿</span><span class='Number'>6</span> <span class='Comment'># Now it's a list!
</span> <span class='Value'>a</span>
⟨ 4 5 6 ⟩
</pre>
<p>But this changes the value of <code><span class='Value'>a</span></code> to a completely unrelated value. What if I want to apply a transformation to <code><span class='Value'>a</span></code>, for example to subtract one? Of course I can write the value <code><span class='Value'>a</span></code> on the right hand side of the assignment, but that's extra work and doesn't really seem to represent what I'm doing, which is conceptually just to apply a function to <code><span class='Value'>a</span></code>. So BQN also has a shorthand, called <em>modified assignment</em>. Here, the modified assignment <code><span class='Function'>-</span><span class='Gets'>↩</span></code> subtracts a value from <code><span class='Value'>a</span></code>.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQphIOKGqSBhIC0gMQphCgphIC3ihqkgMQ==">↗️</a><pre> <span class='Value'>a</span> <span class='Gets'>↩</span> <span class='Value'>a</span> <span class='Function'>-</span> <span class='Number'>1</span>
<span class='Value'>a</span>
⟨ 3 4 5 ⟩
<span class='Value'>a</span> <span class='Function'>-</span><span class='Gets'>↩</span> <span class='Number'>1</span>
⟨ 2 3 4 ⟩
</pre>
<p>(In case you're wondering why I didn't have to write <code><span class='Value'>a</span></code> again that last time, the evaluator suppresses the printed result for ordinary assignments but not modified ones. This is a feature of my website software and not the BQN language). It looks a lot like the special assignment operators <code><span class='Function'>+=</span></code>, <code><span class='Function'>/=</span></code>, and so on that you'll see in C or Javascript. What BQN brings to the table is that you can use any two-argument function at all here, because two-argument function are always written as operators. For example, we can prepend some elements to <code><span class='Value'>a</span></code>:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQphIOKGqSBhIC0gMQphIC3ihqkgMQphIOKIvsuc4oapIDDigL8x">↗️</a><pre> <span class='Value'>a</span> <span class='Function'>∾</span><span class='Modifier'>˜</span><span class='Gets'>↩</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span>
⟨ 0 1 2 3 4 ⟩
</pre>
<p>But what about functions with only one argument? It's possible to do this using a dummy right argument such as the null character, <code><span class='String'>@</span></code>. To turn a function that takes one argument into one that takes two, we can compose it with a function that takes two arguments and returns one of them. The left one, since the variable to modify is on the left hand side. Perhaps… Left? (<code><span class='Function'>⊣</span></code>)?</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQphIOKGqSBhIC0gMQphIC3ihqkgMQphIOKIvsuc4oapIDDigL8xCiJhYmNkIiDijL3iiJjiiqMgInd4eXoiCgphIOKMveKImOKKo+KGqSBA">↗️</a><pre> <span class='String'>"abcd"</span> <span class='Function'>⌽</span><span class='Modifier2'>∘</span><span class='Function'>⊣</span> <span class='String'>"wxyz"</span>
"dcba"
<span class='Value'>a</span> <span class='Function'>⌽</span><span class='Modifier2'>∘</span><span class='Function'>⊣</span><span class='Gets'>↩</span> <span class='String'>@</span>
⟨ 4 3 2 1 0 ⟩
</pre>
<p>But fortunately, there's a simpler syntax as well: write your one-argument function before <code><span class='Gets'>↩</span></code> with no right hand side. Bit of a Yoda vibe: "<code><span class='Value'>a</span></code> reversed is".</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQphIOKGqSBhIC0gMQphIC3ihqkgMQphIOKIvsuc4oapIDDigL8xCmEg4oy94oiY4oqj4oapIEAKYSDijL3ihqkKCmEgNOKKuC3ihqkgICAgICAgICAgICMgQW5kIGJhY2sgYWdhaW4=">↗️</a><pre> <span class='Value'>a</span> <span class='Function'>⌽</span><span class='Gets'>↩</span>
⟨ 0 1 2 3 4 ⟩
<span class='Value'>a</span> <span class='Number'>4</span><span class='Modifier2'>⊸</span><span class='Function'>-</span><span class='Gets'>↩</span> <span class='Comment'># And back again
</span>⟨ 4 3 2 1 0 ⟩
</pre>
<p>Notice that there's no need for parentheses: modifiers bind more strongly than the assignment character. Now what if we want to decrease the last two elements of <code><span class='Value'>a</span></code>? That is, we want to compute the following array while storing it in <code><span class='Value'>a</span></code>.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQphIOKGqSBhIC0gMQphIC3ihqkgMQphIOKIvsuc4oapIDDigL8xCmEg4oy94oiY4oqj4oapIEAKYSDijL3ihqkKYSA04oq4LeKGqSAgICAgICAgICAgIyBBbmQgYmFjayBhZ2Fpbgot4p+cNOKMvijCrzLiirjihpEpIGEKCmEgICAgICAgICAgICAgICAgIyBJdCBoYXNuJ3QgY2hhbmdlZCwgb2YgY291cnNl">↗️</a><pre> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Number'>4</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>¯2</span><span class='Modifier2'>⊸</span><span class='Function'>↑</span><span class='Paren'>)</span> <span class='Value'>a</span>
⟨ 4 3 2 ¯3 ¯4 ⟩
<span class='Value'>a</span> <span class='Comment'># It hasn't changed, of course
</span>⟨ 4 3 2 1 0 ⟩
</pre>
<p>The code to do this looks the same as what we did with Reverse (<code><span class='Function'>⌽</span></code>). Again we don't have to parenthesize the function, because modifiers associate from left to right, so Under (<code><span class='Modifier2'>⌾</span></code>) binds to its operands before Compose (<code><span class='Modifier2'>∘</span></code>) does.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQphIOKGqSBhIC0gMQphIC3ihqkgMQphIOKIvsuc4oapIDDigL8xCmEg4oy94oiY4oqj4oapIEAKYSDijL3ihqkKYSA04oq4LeKGqSAgICAgICAgICAgIyBBbmQgYmFjayBhZ2FpbgphIC3in5w04oy+KMKvMuKKuOKGkSnihqk=">↗️</a><pre> <span class='Value'>a</span> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Number'>4</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>¯2</span><span class='Modifier2'>⊸</span><span class='Function'>↑</span><span class='Paren'>)</span><span class='Gets'>↩</span>
⟨ 4 3 2 ¯3 ¯4 ⟩
</pre>
|