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
|
<head>
<link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/>
<link href="../style.css" rel="stylesheet"/>
<title>BQN: Blocks</title>
</head>
<div class="nav">(<a href="https://github.com/mlochbaum/BQN">github</a>) / <a href="../index.html">BQN</a> / <a href="index.html">doc</a></div>
<h1 id="blocks"><a class="header" href="#blocks">Blocks</a></h1>
<p>In BQN, a <em>block</em> is any piece of code surrounded with curly braces <code><span class='Brace'>{}</span></code>. Blocks can be used simply to group statements, or can define functions or modifiers. They are the sole large-scale structure used to organize programs. An important aspect of organization is <a href="namespace.html">namespaces</a>, which are created with blocks but not discussed on this page.</p>
<p>Blocks are most commonly used to define functions by including one of the special names for arguments, <code><span class='Value'>π¨</span></code> or <code><span class='Value'>π©</span></code>. With the operands <code><span class='Function'>π½</span></code> or <code><span class='Function'>πΎ</span></code>, they can also define 1-modifiers or 2-modifiers.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=e/CdlakrMX0gMwrDl3vwnZWp8J2UvfCdlal9IDQ=">βοΈ</a><pre> <span class='Brace'>{</span><span class='Value'>π©</span><span class='Function'>+</span><span class='Number'>1</span><span class='Brace'>}</span> <span class='Number'>3</span>
4
<span class='Function'>Γ</span><span class='Brace'>{</span><span class='Value'>π©</span><span class='Function'>π½</span><span class='Value'>π©</span><span class='Brace'>}</span> <span class='Number'>4</span>
16
</pre>
<p>Because they use <a href="lexical.html">lexical scoping</a>, blocks can also be used to encapsulate code. If a block uses only variables that it initializes, then it has no dependence on its environment and would work the same way if defined anywhere. But it can also use external variables, defined in a containing block.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YeKGkGLihpAib3V0ZXIiCnsgYeKGkCJpbm5lciIg4ouEIGHigL9iIH0=">βοΈ</a><pre> <span class='Value'>a</span><span class='Gets'>β</span><span class='Value'>b</span><span class='Gets'>β</span><span class='String'>"outer"</span>
<span class='Brace'>{</span> <span class='Value'>a</span><span class='Gets'>β</span><span class='String'>"inner"</span> <span class='Separator'>β</span> <span class='Value'>a</span><span class='Ligature'>βΏ</span><span class='Value'>b</span> <span class='Brace'>}</span>
β¨ "inner" "outer" β©
</pre>
<h2 id="headerless-blocks"><a class="header" href="#headerless-blocks">Headerless blocks</a></h2>
<p>In the simplest case a block is just a list of statements, which are executed to <em>evaluate</em> the block. A block with no special names like <code><span class='Value'>π¨</span></code> or <code><span class='Value'>π©</span></code> is called an <em>immediate block</em>, and is evaluated as soon as it is reached. The only thing such a block does is group some statements, and create a scope for them so that definitions made there are discarded when the block finishes. Even this small amount of functionality could be useful; as an example the following program can build up an array from named components without polluting the rest of the program with those names.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dXBkb3duIOKGkCB7IHVw4oaQ4oaVNSDii4QgZG93buKGkOKMvXVwIOKLhCB1cOKIvmRvd24gfQp1cGRvd24=">βοΈ</a><pre> <span class='Value'>updown</span> <span class='Gets'>β</span> <span class='Brace'>{</span> <span class='Value'>up</span><span class='Gets'>β</span><span class='Function'>β</span><span class='Number'>5</span> <span class='Separator'>β</span> <span class='Value'>down</span><span class='Gets'>β</span><span class='Function'>β½</span><span class='Value'>up</span> <span class='Separator'>β</span> <span class='Value'>up</span><span class='Function'>βΎ</span><span class='Value'>down</span> <span class='Brace'>}</span>
<span class='Value'>updown</span>
β¨ 0 1 2 3 4 4 3 2 1 0 β©
</pre>
<p>An immediate block is only ever evaluated once, and can't be used for control flow in a program. Including special names in a headerless block lets us define functions and modifiers, which have a broader range of uses. All special names are listed below:</p>
<table>
<thead>
<tr>
<th>Lowercase</th>
<th>Uppercase</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Value'>π©</span></code></td>
<td><code><span class='Function'>π</span></code></td>
<td>Right <a href="#arguments">argument</a></td>
</tr>
<tr>
<td><code><span class='Value'>π¨</span></code></td>
<td><code><span class='Function'>π</span></code></td>
<td>Left <a href="#arguments">argument</a>, or <a href="expression.html#nothing">Nothing</a> (<code><span class='Nothing'>Β·</span></code>)</td>
</tr>
<tr>
<td><code><span class='Value'>π€</span></code></td>
<td><code><span class='Function'>π</span></code></td>
<td>Function <a href="#self-reference">self-reference</a></td>
</tr>
<tr>
<td><code><span class='Value'>π</span></code></td>
<td><code><span class='Function'>π½</span></code></td>
<td>Left <a href="#operands">operand</a></td>
</tr>
<tr>
<td><code><span class='Value'>π</span></code></td>
<td><code><span class='Function'>πΎ</span></code></td>
<td>Right <a href="#operands">operand</a></td>
</tr>
<tr>
<td><code><span class='Value'>π£</span></code></td>
<td>none</td>
<td>Modifier <a href="#self-reference">self-reference</a></td>
</tr>
</tbody>
</table>
<p>Of these, <code><span class='Value'>π£</span></code> is sort of a "more special" character, as we'll discuss below. Except for <code><span class='Value'>π£</span></code>, every special name is a single character and can't have underscores added to spell it as a modifier. This allows a modifier to be applied to a special name with no spacing, as in <code><span class='Value'>π</span><span class='Modifier'>_m</span></code>, where it couldn't be with ordinary names.</p>
<h3 id="arguments"><a class="header" href="#arguments">Arguments</a></h3>
<p>The names <code><span class='Value'>π¨</span></code> and <code><span class='Value'>π©</span></code>, and their uppercase spellings, represent function arguments. As the argument to a function is typically data, it's more common to use the lowercase forms for these. Having either of these names turns an immediate block into a function (or an immediate modifier into a deferred one; see the next section). Instead of being evaluated as soon as it appears in the source, a function is evaluated when it's called, with the special names set to appropriate values. Their values can be changed like ordinary variables.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eydjJz3wnZWpfSAiYWJjZCIKeyDwnZWpK+KGqTIg4ouEIDDiiY3wnZWpIH0gMwo0IHsg4p+o8J2VqeKLhC3wnZWo4p+pIH0gNQ==">βοΈ</a><pre> <span class='Brace'>{</span><span class='String'>'c'</span><span class='Function'>=</span><span class='Value'>π©</span><span class='Brace'>}</span> <span class='String'>"abcd"</span>
β¨ 0 0 1 0 β©
<span class='Brace'>{</span> <span class='Value'>π©</span><span class='Function'>+</span><span class='Gets'>β©</span><span class='Number'>2</span> <span class='Separator'>β</span> <span class='Number'>0</span><span class='Function'>β</span><span class='Value'>π©</span> <span class='Brace'>}</span> <span class='Number'>3</span>
β¨ 0 5 β©
<span class='Number'>4</span> <span class='Brace'>{</span> <span class='Bracket'>β¨</span><span class='Value'>π©</span><span class='Separator'>β</span><span class='Function'>-</span><span class='Value'>π¨</span><span class='Bracket'>β©</span> <span class='Brace'>}</span> <span class='Number'>5</span>
β¨ 5 Β―4 β©
</pre>
<p>A function with <code><span class='Value'>π¨</span></code> in its definition doesn't have to be called with two arguments. If it has only one, then <code><span class='Value'>π¨</span></code> is given the special value <a href="expression.html#nothing">Nothing</a>, or <code><span class='Nothing'>Β·</span></code>. This is the only time a variable can ever be Nothing, as an assignment such as <code><span class='Value'>v</span><span class='Gets'>β</span><span class='Nothing'>Β·</span></code> is not allowed.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyB7ICgyw5fwnZWoKS3wnZWpIH0gMQogIHsgKDLDl/CdlagpLfCdlakgfSAx">βοΈ</a><pre> <span class='Number'>3</span> <span class='Brace'>{</span> <span class='Paren'>(</span><span class='Number'>2</span><span class='Function'>Γ</span><span class='Value'>π¨</span><span class='Paren'>)</span><span class='Function'>-</span><span class='Value'>π©</span> <span class='Brace'>}</span> <span class='Number'>1</span>
5
<span class='Brace'>{</span> <span class='Paren'>(</span><span class='Number'>2</span><span class='Function'>Γ</span><span class='Value'>π¨</span><span class='Paren'>)</span><span class='Function'>-</span><span class='Value'>π©</span> <span class='Brace'>}</span> <span class='Number'>1</span>
Β―1
</pre>
<p>In the second function, <code><span class='Value'>π¨</span></code> behaves just like <code><span class='Nothing'>Β·</span></code>, so that the function <code><span class='Number'>2</span><span class='Function'>Γ</span><span class='Value'>π¨</span></code> is not evaluated and <code><span class='Function'>-</span></code> doesn't have a left argument. It has a similar effect when used as the left argument to a function in a train.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ImFiYyIgeyAo8J2VqOKJjeKMvSkg8J2VqSB9ICJkZWYiCiAgICAgIHsgKPCdlajiiY3ijL0pIPCdlakgfSAiZGVmIg==">βοΈ</a><pre> <span class='String'>"abc"</span> <span class='Brace'>{</span> <span class='Paren'>(</span><span class='Value'>π¨</span><span class='Function'>ββ½</span><span class='Paren'>)</span> <span class='Value'>π©</span> <span class='Brace'>}</span> <span class='String'>"def"</span>
ββ
β΅"abc
fed"
β
<span class='Brace'>{</span> <span class='Paren'>(</span><span class='Value'>π¨</span><span class='Function'>ββ½</span><span class='Paren'>)</span> <span class='Value'>π©</span> <span class='Brace'>}</span> <span class='String'>"def"</span>
ββ
β΅"fed"
β
</pre>
<p>However, <code><span class='Nothing'>Β·</span></code> can only be used as an argument, and not a list element or operand. Don't use <code><span class='Value'>π¨</span></code> in these ways in a function that could be called monadically. Another potential issue is that <code><span class='Modifier2'>βΈ</span></code> and <code><span class='Modifier2'>β</span></code> don't work the way you might expect.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eyDwnZWoIOKLhuKKuC0g8J2VqSB9IDU=">βοΈ</a><pre> <span class='Brace'>{</span> <span class='Value'>π¨</span> <span class='Function'>β</span><span class='Modifier2'>βΈ</span><span class='Function'>-</span> <span class='Value'>π©</span> <span class='Brace'>}</span> <span class='Number'>5</span>
143.4131591025766
</pre>
<p>Called dyadically, this function will expand to <code><span class='Paren'>(</span><span class='Function'>β</span><span class='Value'>π¨</span><span class='Paren'>)</span><span class='Function'>-</span><span class='Value'>π©</span></code>, so we might expect the monadic result to be <code><span class='Function'>-</span><span class='Value'>π©</span></code>. This sort of expansion isn't right with <code><span class='Nothing'>Β·</span></code> on the left. <code><span class='Function'>β</span><span class='Modifier2'>βΈ</span><span class='Function'>-</span></code> taken as a whole is a function, so <code><span class='Nothing'>Β·</span> <span class='Function'>β</span><span class='Modifier2'>βΈ</span><span class='Function'>-</span> <span class='Value'>π©</span></code> is just <code><span class='Function'>β</span><span class='Modifier2'>βΈ</span><span class='Function'>-</span> <span class='Value'>π©</span></code>, or <code><span class='Paren'>(</span><span class='Function'>β</span><span class='Value'>π©</span><span class='Paren'>)</span><span class='Function'>-</span><span class='Value'>π©</span></code>, giving the large result seen above.</p>
<h3 id="operands"><a class="header" href="#operands">Operands</a></h3>
<p>The special names <code><span class='Function'>π½</span></code> and <code><span class='Function'>πΎ</span></code>, and their lowercase forms, represent operands. Since operands are more often functions, they're typically shown with the uppercase spelling. If <code><span class='Function'>π½</span></code> is present in a block then it defines a 1-modifier or 2-modifier depending on whether <code><span class='Function'>πΎ</span></code> is present; if <code><span class='Function'>πΎ</span></code> is there it's always a 2-modifier.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=NCB7w5fLnPCdlZd9CjIge/CdlZcr8J2VmH0gMw==">βοΈ</a><pre> <span class='Number'>4</span> <span class='Brace'>{</span><span class='Function'>Γ</span><span class='Modifier'>Λ</span><span class='Value'>π</span><span class='Brace'>}</span>
16
<span class='Number'>2</span> <span class='Brace'>{</span><span class='Value'>π</span><span class='Function'>+</span><span class='Value'>π</span><span class='Brace'>}</span> <span class='Number'>3</span>
5
</pre>
<p>As shown above, modifiers without <code><span class='Value'>π¨</span></code>, <code><span class='Value'>π©</span></code>, or <code><span class='Value'>π€</span></code> behave essentially like functions with a higher precedence. These <em>immediate modifiers</em> take operands and return a result of any type. The result is given a function role, so it's most common to return a function, rather than a number as shown above.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=X2RvdF8g4oaQIHvwnZS9wrTiiJjwnZS+fQox4oC/MuKAvzMgK19kb3Rfw5cgMeKAvzDigL8x">βοΈ</a><pre> <span class='Modifier2'>_dot_</span> <span class='Gets'>β</span> <span class='Brace'>{</span><span class='Function'>π½</span><span class='Modifier'>Β΄</span><span class='Modifier2'>β</span><span class='Function'>πΎ</span><span class='Brace'>}</span>
<span class='Number'>1</span><span class='Ligature'>βΏ</span><span class='Number'>2</span><span class='Ligature'>βΏ</span><span class='Number'>3</span> <span class='Function'>+</span><span class='Modifier2'>_dot_</span><span class='Function'>Γ</span> <span class='Number'>1</span><span class='Ligature'>βΏ</span><span class='Number'>0</span><span class='Ligature'>βΏ</span><span class='Number'>1</span>
4
</pre>
<p>However, if one of these names is included, then a <em>deferred modifier</em> is created instead: rather than evaluate the contents when the modifier is called, the operands are bound to it to create a derived function. When this function is called, the statements in the block are evaluated.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K3vwnZWp8J2UvfCdlal9IDYKMiDipYp74p+o8J2UvfCdlags8J2UvvCdlanin6l9LSA1">βοΈ</a><pre> <span class='Function'>+</span><span class='Brace'>{</span><span class='Value'>π©</span><span class='Function'>π½</span><span class='Value'>π©</span><span class='Brace'>}</span> <span class='Number'>6</span>
12
<span class='Number'>2</span> <span class='Function'>β₯</span><span class='Brace'>{</span><span class='Bracket'>β¨</span><span class='Function'>π½</span><span class='Value'>π¨</span><span class='Separator'>,</span><span class='Function'>πΎ</span><span class='Value'>π©</span><span class='Bracket'>β©</span><span class='Brace'>}</span><span class='Function'>-</span> <span class='Number'>5</span>
β¨ β¨ 2 β© Β―5 β©
</pre>
<p>The distinction between an immediate and deferred modifier only matters inside the braces. Once defined, the object is simply a modifier that can be called on operands to return a result. For a deferred modifier this result will always be a function; for an immediate modifier it could be anything.</p>
<h3 id="self-reference"><a class="header" href="#self-reference">Self-reference</a></h3>
<p>If a block is assigned a name after it is created, this name can be used for recursion:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RmFjdCDihpAgeyDwnZWpIMOXICgw4oq4PCnil7Yx4oC/RmFjdCDwnZWpLTEgfQpGYWN0IDcKKMOXwrQxK+KGlSkgNyAgIyBUaGVyZSdzIG9mdGVuIGEgc2ltcGxlciBzb2x1dGlvbiB0aGFuIHJlY3Vyc2lvbg==">βοΈ</a><pre> <span class='Function'>Fact</span> <span class='Gets'>β</span> <span class='Brace'>{</span> <span class='Value'>π©</span> <span class='Function'>Γ</span> <span class='Paren'>(</span><span class='Number'>0</span><span class='Modifier2'>βΈ</span><span class='Function'><</span><span class='Paren'>)</span><span class='Modifier2'>βΆ</span><span class='Number'>1</span><span class='Ligature'>βΏ</span><span class='Function'>Fact</span> <span class='Value'>π©</span><span class='Function'>-</span><span class='Number'>1</span> <span class='Brace'>}</span>
<span class='Function'>Fact</span> <span class='Number'>7</span>
5040
<span class='Paren'>(</span><span class='Function'>Γ</span><span class='Modifier'>Β΄</span><span class='Number'>1</span><span class='Function'>+β</span><span class='Paren'>)</span> <span class='Number'>7</span> <span class='Comment'># There's often a simpler solution than recursion
</span>5040
</pre>
<p>This is somewhat unsatisfying because it is external to the function being defined, even though it doesn't depend on outside information. Instead, the special name <code><span class='Function'>π</span></code> can be used to refer to the function it appears in. This allows anonymous recursive functions to be defined.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eyDwnZWpIMOXICgw4oq4PCnil7Yx4oC/8J2ViiDwnZWpLTEgfSA3">βοΈ</a><pre> <span class='Brace'>{</span> <span class='Value'>π©</span> <span class='Function'>Γ</span> <span class='Paren'>(</span><span class='Number'>0</span><span class='Modifier2'>βΈ</span><span class='Function'><</span><span class='Paren'>)</span><span class='Modifier2'>βΆ</span><span class='Number'>1</span><span class='Ligature'>βΏ</span><span class='Function'>π</span> <span class='Value'>π©</span><span class='Function'>-</span><span class='Number'>1</span> <span class='Brace'>}</span> <span class='Number'>7</span>
5040
</pre>
<p>For modifiers, <code><span class='Value'>π£</span></code> refers to the containing modifier. <code><span class='Function'>π</span></code> makes the modifier a deferred modifier like <code><span class='Value'>π¨</span></code> and <code><span class='Value'>π©</span></code> do, and refers to the derived function. For example, this tail-recursive factorial function uses the operand to accumulate a result, a task that is usually done with a second <code><span class='Value'>factorial_helper</span></code> function in elementary Scheme.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RmFjdF9tb2Qg4oaQIDEgeyAoMOKKuDwp4pe24p+o8J2VlywgKPCdlZfDl/CdlakpX/CdlaPin6kg8J2VqS0xIH0KRmFjdF9tb2QgNw==">βοΈ</a><pre> <span class='Function'>Fact_mod</span> <span class='Gets'>β</span> <span class='Number'>1</span> <span class='Brace'>{</span> <span class='Paren'>(</span><span class='Number'>0</span><span class='Modifier2'>βΈ</span><span class='Function'><</span><span class='Paren'>)</span><span class='Modifier2'>βΆ</span><span class='Bracket'>β¨</span><span class='Value'>π</span><span class='Separator'>,</span> <span class='Paren'>(</span><span class='Value'>π</span><span class='Function'>Γ</span><span class='Value'>π©</span><span class='Paren'>)</span><span class='Modifier'>_π£</span><span class='Bracket'>β©</span> <span class='Value'>π©</span><span class='Function'>-</span><span class='Number'>1</span> <span class='Brace'>}</span>
<span class='Function'>Fact_mod</span> <span class='Number'>7</span>
5040
</pre>
<p>Because <code><span class='Value'>π£</span></code> only ever refers to a 1-modifier or 2-modifer, it can never make sense to refer to it as a function, and the uppercase letter <code><span class='Value'>β</span></code> is not recognized by BQN. In order to allow <code><span class='Value'>π£</span></code> to be spelled as a 1-modifier <code><span class='Modifier'>_π£</span></code> or 2-modifier <code><span class='Modifier2'>_π£_</span></code>, it is treated as an ordinary identifier character, so it must be separated from letters or numbers by spaces.</p>
<h2 id="block-headers"><a class="header" href="#block-headers">Block headers</a></h2>
<p>As a program becomes larger, it often becomes necessary to name inputs to blocks rather than just using special names. It can also become difficult to identify what kind of block is being defined, as it requires scanning through the block for special names. A <em>block header</em>, which is separated from the body of a block by a colon <code><span class='Head'>:</span></code>, specifies the kind of block and can declare names for the block and its inputs.</p>
<pre><span class='Function'>Fact</span> <span class='Gets'>β</span> <span class='Brace'>{</span> <span class='Function'>F</span> <span class='Value'>n</span><span class='Head'>:</span>
<span class='Value'>n</span> <span class='Function'>Γ</span> <span class='Paren'>(</span><span class='Number'>0</span><span class='Modifier2'>βΈ</span><span class='Function'><</span><span class='Paren'>)</span><span class='Modifier2'>βΆ</span><span class='Number'>1</span><span class='Ligature'>βΏ</span><span class='Function'>F</span> <span class='Value'>n</span><span class='Function'>-</span><span class='Number'>1</span>
<span class='Brace'>}</span>
</pre>
<p>Its syntax mirrors an application of the block. As suggested by the positioning, the names given in a header apply only inside the block: for example <code><span class='Function'>F</span></code> above is only defined inside the <code><span class='Brace'>{}</span></code> braces while <code><span class='Function'>Fact</span></code> could be used either outside or inside. Some other possibilites are given below.</p>
<pre><span class='Comment'># A dyadic function that refers to itself as Func
</span><span class='Brace'>{</span> <span class='Value'>l</span> <span class='Function'>Func</span> <span class='Value'>r</span><span class='Head'>:</span>
<span class='Value'>β¦</span>
<span class='Comment'># A deferred 1-modifier with a list argument
</span><span class='Brace'>{</span> <span class='Function'>Fn</span> <span class='Modifier'>_apply</span> <span class='Bracket'>β¨</span><span class='Value'>a</span><span class='Separator'>,</span><span class='Value'>b</span><span class='Bracket'>β©</span><span class='Head'>:</span>
<span class='Value'>β¦</span>
<span class='Comment'># A monadic function with no names given
</span><span class='Brace'>{</span> <span class='Function'>π</span><span class='Value'>π©</span><span class='Head'>:</span>
<span class='Value'>β¦</span>
<span class='Comment'># An immediate 2-modifier with some destructuring
</span><span class='Brace'>{</span> <span class='Function'>F</span> <span class='Modifier2'>_op_</span> <span class='Nothing'>Β·</span><span class='Ligature'>βΏ</span><span class='Value'>val</span><span class='Head'>:</span>
<span class='Value'>β¦</span>
</pre>
<p>In all cases special names still work just like in a headerless function. In this respect the effect of the header is the same as a series of assignments at the beginning of a function, such as the following translation of the second header above:</p>
<pre><span class='Brace'>{</span> <span class='Comment'># Fn _apply β¨a,bβ©:
</span> <span class='Function'>Fn</span> <span class='Gets'>β</span> <span class='Function'>π½</span>
<span class='Modifier'>_apply</span> <span class='Gets'>β</span> <span class='Modifier'>_π£</span>
<span class='Bracket'>β¨</span><span class='Value'>a</span><span class='Separator'>,</span><span class='Value'>b</span><span class='Bracket'>β©</span> <span class='Gets'>β</span> <span class='Value'>π©</span>
<span class='Value'>β¦</span>
</pre>
<p>Unlike these assignments, the header also constrains what inputs the block can take: a monadic 1-modifier like the one above can't take a right operand or left argument, and consequently its body can't contain <code><span class='Function'>πΎ</span></code> or <code><span class='Value'>π¨</span></code>. Calling it with a left argument, or a right argument that isn't a two-element list, will result in an error.</p>
<h3 id="destructuring"><a class="header" href="#destructuring">Destructuring</a></h3>
<p>Arguments and operands allow <a href="expression.html#destructuring">destructuring</a> like assignment does. While assignment only tolerates lists of variables, header destructuring also allows constants. The argument must match the given structure, including the constants where they appear, or an error results.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RGVzdHJ1Y3Qg4oaQIHsg8J2ViiBh4oC/MeKAv+KfqGIswrcsMuKfqTogYeKJjWIgfQpEZXN0cnVjdCAgICAgICA14oC/MeKAv+KfqDcsz4AsMuKfqQ==">βοΈ</a><pre> <span class='Function'>Destruct</span> <span class='Gets'>β</span> <span class='Brace'>{</span> <span class='Function'>π</span> <span class='Value'>a</span><span class='Ligature'>βΏ</span><span class='Number'>1</span><span class='Ligature'>βΏ</span><span class='Bracket'>β¨</span><span class='Value'>b</span><span class='Separator'>,</span><span class='Nothing'>Β·</span><span class='Separator'>,</span><span class='Number'>2</span><span class='Bracket'>β©</span><span class='Head'>:</span> <span class='Value'>a</span><span class='Function'>β</span><span class='Value'>b</span> <span class='Brace'>}</span>
<span class='Function'>Destruct</span> <span class='Number'>5</span><span class='Ligature'>βΏ</span><span class='Number'>1</span><span class='Ligature'>βΏ</span><span class='Bracket'>β¨</span><span class='Number'>7</span><span class='Separator'>,</span><span class='Number'>Ο</span><span class='Separator'>,</span><span class='Number'>2</span><span class='Bracket'>β©</span>
β¨ 5 7 β©
</pre>
<h3 id="special-names-in-headers"><a class="header" href="#special-names-in-headers">Special names in headers</a></h3>
<p>Any element of a function or modifier header can be left nameless by using the corresponding special name in that position, instead of an identifier. For example, the header <code><span class='Value'>π¨</span> <span class='Function'>π½</span><span class='Modifier2'>_π£_</span><span class='Function'>πΎ</span> <span class='Value'>π©</span><span class='Head'>:</span></code> incorporates as much vagueness as possible. It indicates a deferred 2-modifier, but provides no other information.</p>
<p>The name <code><span class='Value'>π¨</span></code> in this context can refer to either a left argument or no left argument, allowing a header with arguments to be used even for an ambiguous function. Recall that <code><span class='Value'>π¨</span></code> is the only token other than <code><span class='Nothing'>Β·</span></code> that can have no value. If an identifier or list is given as the left argument, then the function must be called with a left argument.</p>
<h3 id="short-headers"><a class="header" href="#short-headers">Short headers</a></h3>
<p>A header can also be a plain name with no inputs, called a <em>label</em>. A label specifies the type of the block and gives an internal name that can be used to refer to it, but doesn't specify the inputs.</p>
<pre><span class='Brace'>{</span> <span class='Value'>b</span><span class='Head'>:</span> <span class='Comment'># Block
</span><span class='Brace'>{</span> <span class='Function'>π</span><span class='Head'>:</span> <span class='Comment'># Function
</span><span class='Brace'>{</span> <span class='Modifier'>_π£</span><span class='Head'>:</span> <span class='Comment'># 1-Modifier
</span><span class='Brace'>{</span> <span class='Modifier2'>_π£_</span><span class='Head'>:</span> <span class='Comment'># 2-Modifier
</span></pre>
<p>For immediate blocks, this is the only type of header possible, and it must use an identifier as there is no applicable special name. However, the name can't be used in code: it doesn't make sense to refer to a value while it is still being computed!</p>
<h2 id="multiple-bodies"><a class="header" href="#multiple-bodies">Multiple bodies</a></h2>
<p>Blocks can include more than one body, separated by semicolons <code><span class='Head'>;</span></code>. The body used for a particular evaluation is chosen based on the arguments the the block. One special case is that functions and deferred modifiers can have two headerless bodies (that is, no headers or predicatesβsee below): the first applies when there's one argument and the second when there are two.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=QW1iaXYg4oaQIHsg4p+oMSzwnZWp4p+pIDsg4p+oMizwnZWoLPCdlanin6kgfQpBbWJpdiAnYScKJ2EnIEFtYml2ICdiJw==">βοΈ</a><pre> <span class='Function'>Ambiv</span> <span class='Gets'>β</span> <span class='Brace'>{</span> <span class='Bracket'>β¨</span><span class='Number'>1</span><span class='Separator'>,</span><span class='Value'>π©</span><span class='Bracket'>β©</span> <span class='Head'>;</span> <span class='Bracket'>β¨</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Value'>π¨</span><span class='Separator'>,</span><span class='Value'>π©</span><span class='Bracket'>β©</span> <span class='Brace'>}</span>
<span class='Function'>Ambiv</span> <span class='String'>'a'</span>
β¨ 1 'a' β©
<span class='String'>'a'</span> <span class='Function'>Ambiv</span> <span class='String'>'b'</span>
β¨ 2 'a' 'b' β©
</pre>
<p>Bodies with headers come before any that don't have them. When a block is called, its headers are checked in order for compatibility with the arguments, and the first body with a compatible header is used.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Q2FzZUFkZCDihpAgeyAy8J2VijM6MOKAvzUgOyAy8J2VivCdlak64p+oMSwyK/Cdlanin6kgOyDwnZWK8J2VqToy4oC/8J2VqSB9CjIgQ2FzZUFkZCAzCjIgQ2FzZUFkZCA0CiAgQ2FzZUFkZCA0">βοΈ</a><pre> <span class='Function'>CaseAdd</span> <span class='Gets'>β</span> <span class='Brace'>{</span> <span class='Number'>2</span><span class='Function'>π</span><span class='Number'>3</span><span class='Head'>:</span><span class='Number'>0</span><span class='Ligature'>βΏ</span><span class='Number'>5</span> <span class='Head'>;</span> <span class='Number'>2</span><span class='Function'>π</span><span class='Value'>π©</span><span class='Head'>:</span><span class='Bracket'>β¨</span><span class='Number'>1</span><span class='Separator'>,</span><span class='Number'>2</span><span class='Function'>+</span><span class='Value'>π©</span><span class='Bracket'>β©</span> <span class='Head'>;</span> <span class='Function'>π</span><span class='Value'>π©</span><span class='Head'>:</span><span class='Number'>2</span><span class='Ligature'>βΏ</span><span class='Value'>π©</span> <span class='Brace'>}</span>
<span class='Number'>2</span> <span class='Function'>CaseAdd</span> <span class='Number'>3</span>
β¨ 0 5 β©
<span class='Number'>2</span> <span class='Function'>CaseAdd</span> <span class='Number'>4</span>
β¨ 1 6 β©
<span class='Function'>CaseAdd</span> <span class='Number'>4</span>
β¨ 2 4 β©
</pre>
<p>If no header is compatible, the call results in an error.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyBDYXNlQWRkIDM=">βοΈ</a><pre> <span class='Number'>3</span> <span class='Function'>CaseAdd</span> <span class='Number'>3</span>
<span class='Error'>Error: No header matched arguments</span>
</pre>
<h3 id="case-headers"><a class="header" href="#case-headers">Case headers</a></h3>
<p>A special rule allows for convenient case-matching syntax for one-argument functions. In any function header with one argument, the function name can be omitted as long as the argument is <em>not</em> a plain identifierβit must be <code><span class='Value'>π©</span></code> or a compound value like a list to distinguish it from an immediate block label.</p>
<pre><span class='Function'>Test</span> <span class='Gets'>β</span> <span class='Brace'>{</span>
<span class='String'>"abc"</span><span class='Head'>:</span> <span class='String'>"string"</span> <span class='Head'>;</span>
<span class='Bracket'>β¨</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Value'>b</span><span class='Bracket'>β©</span><span class='Head'>:</span> <span class='Function'>β½</span><span class='Value'>π©</span> <span class='Head'>;</span>
<span class='Number'>5</span><span class='Head'>:</span> <span class='String'>"number"</span> <span class='Head'>;</span>
<span class='Value'>π©</span><span class='Head'>:</span> <span class='String'>"default"</span>
<span class='Brace'>}</span>
</pre>
<p>These case-style headers function exactly the same as if they were preceded by <code><span class='Function'>π</span></code>, and can be mixed with other kinds of headers.</p>
<h3 id="predicates"><a class="header" href="#predicates">Predicates</a></h3>
<p>Destructuring with a header is quite limited, only allowing matching structure and data with exact equality. A predicate, written with <code><span class='Head'>?</span></code>, allows you to test an arbitrary property before evaluating the rest of the body, and also serves as a limited kind of control flow. It can be thought of as an extension to a header, so that for example the following function requires the argument to have two elements and for the first to be less than the second before using the first body. Otherwise it moves to the next body, which is unconditional.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Q2hlY2tQYWlyIOKGkCB7IPCdlYrin6hhLGLin6k6IGE8Yj8gIm9rIiA7ICJub3Qgb2siIH0KCkNoZWNrUGFpciDin6gzLDjin6kgICAgIyBGYWlscyBkZXN0cnVjdHVyaW5nCkNoZWNrUGFpciDin6gxLDQsNeKfqSAgIyBOb3QgYSBwYWlyCkNoZWNrUGFpciDin6gzLMKvMeKfqSAgICMgTm90IGFzY2VuZGluZw==">βοΈ</a><pre> <span class='Function'>CheckPair</span> <span class='Gets'>β</span> <span class='Brace'>{</span> <span class='Function'>π</span><span class='Bracket'>β¨</span><span class='Value'>a</span><span class='Separator'>,</span><span class='Value'>b</span><span class='Bracket'>β©</span><span class='Head'>:</span> <span class='Value'>a</span><span class='Function'><</span><span class='Value'>b</span><span class='Head'>?</span> <span class='String'>"ok"</span> <span class='Head'>;</span> <span class='String'>"not ok"</span> <span class='Brace'>}</span>
<span class='Function'>CheckPair</span> <span class='Bracket'>β¨</span><span class='Number'>3</span><span class='Separator'>,</span><span class='Number'>8</span><span class='Bracket'>β©</span> <span class='Comment'># Fails destructuring
</span>"ok"
<span class='Function'>CheckPair</span> <span class='Bracket'>β¨</span><span class='Number'>1</span><span class='Separator'>,</span><span class='Number'>4</span><span class='Separator'>,</span><span class='Number'>5</span><span class='Bracket'>β©</span> <span class='Comment'># Not a pair
</span>"not ok"
<span class='Function'>CheckPair</span> <span class='Bracket'>β¨</span><span class='Number'>3</span><span class='Separator'>,</span><span class='Number'>Β―1</span><span class='Bracket'>β©</span> <span class='Comment'># Not ascending
</span>"not ok"
</pre>
<p>The body where the predicate appears doesn't need to start with a header, and there can be other statements before it. In fact, <code><span class='Head'>?</span></code> functions just like a separator (like <code><span class='Separator'>β</span></code> or <code><span class='Separator'>,</span></code>) with a side effect.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eyBy4oaQ4oy98J2VqSDii4QgJ3QnPeKKkXIgPyByIDsg8J2VqSB9wqggInRlc3Qi4oC/InRoaXMi">βοΈ</a><pre> <span class='Brace'>{</span> <span class='Value'>r</span><span class='Gets'>β</span><span class='Function'>β½</span><span class='Value'>π©</span> <span class='Separator'>β</span> <span class='String'>'t'</span><span class='Function'>=β</span><span class='Value'>r</span> <span class='Head'>?</span> <span class='Value'>r</span> <span class='Head'>;</span> <span class='Value'>π©</span> <span class='Brace'>}</span><span class='Modifier'>Β¨</span> <span class='String'>"test"</span><span class='Ligature'>βΏ</span><span class='String'>"this"</span>
β¨ "tset" "this" β©
</pre>
<p>So <code><span class='Value'>r</span></code> is the reversed argument, and if its first character (the last one in <code><span class='Value'>π©</span></code>) is <code><span class='String'>'t'</span></code> then it returns <code><span class='Value'>r</span></code>, and otherwise we abandon that line of reasoning and return <code><span class='Value'>π©</span></code>. This sounds a lot like an if statement. And <code><span class='Brace'>{</span> <span class='Value'>a</span><span class='Function'><</span><span class='Value'>b</span> <span class='Head'>?</span> <span class='Value'>a</span> <span class='Head'>;</span> <span class='Value'>b</span> <span class='Brace'>}</span></code>, which computes <code><span class='Value'>a</span><span class='Function'>β</span><span class='Value'>b</span></code> the hard way, shows how the syntax can be similar to a ternary operator. This is an immediate block with multiple bodies, something that makes sense with predicates but not headers. But <code><span class='Head'>?;</span></code> offers more possibilities. It can support any number of options, with multiple tests for each oneβthe structure below is "if _ and _ then _; else if _ then _; else _".</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=VGhpbmcg4oaQIHsg8J2VqeKJpTM/IPCdlaniiaQ4PyAyfPCdlakgOyDwnZWpPTA/IEAgOyDiiJ4gfQoKKOKKoiDiiY0gVGhpbmfCqCkg4oaVMTAgICMgVGFibGUgb2YgYXJndW1lbnRzIGFuZCByZXN1bHRz">βοΈ</a><pre> <span class='Function'>Thing</span> <span class='Gets'>β</span> <span class='Brace'>{</span> <span class='Value'>π©</span><span class='Function'>β₯</span><span class='Number'>3</span><span class='Head'>?</span> <span class='Value'>π©</span><span class='Function'>β€</span><span class='Number'>8</span><span class='Head'>?</span> <span class='Number'>2</span><span class='Function'>|</span><span class='Value'>π©</span> <span class='Head'>;</span> <span class='Value'>π©</span><span class='Function'>=</span><span class='Number'>0</span><span class='Head'>?</span> <span class='String'>@</span> <span class='Head'>;</span> <span class='Number'>β</span> <span class='Brace'>}</span>
<span class='Paren'>(</span><span class='Function'>β’</span> <span class='Function'>β</span> <span class='Function'>Thing</span><span class='Modifier'>Β¨</span><span class='Paren'>)</span> <span class='Function'>β</span><span class='Number'>10</span> <span class='Comment'># Table of arguments and results
</span>ββ
β΅ 0 1 2 3 4 5 6 7 8 9
@ β β 1 0 1 0 1 0 β
β
</pre>
<p>This structure is still constrained by the rules of block bodies: each instance of <code><span class='Head'>;</span></code> is a separate scope, so that variables defined before a <code><span class='Head'>?</span></code> don't survive past the <code><span class='Head'>;</span></code>.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eyAwPW7ihpDiiaDwnZWpID8g4oieIDsgbiB9ICJhYmMi">βοΈ</a><pre> <span class='Brace'>{</span> <span class='Number'>0</span><span class='Function'>=</span><span class='Value'>n</span><span class='Gets'>β</span><span class='Function'>β </span><span class='Value'>π©</span> <span class='Head'>?</span> <span class='Number'>β</span> <span class='Head'>;</span> <span class='Value'>n</span> <span class='Brace'>}</span> <span class='String'>"abc"</span>
<span class='Error'>Error: Undefined identifier</span>
</pre>
<p>This is the main drawback of predicates relative to guards in APL dfns (also written with <code><span class='Head'>?</span></code>), while the advantage is that it allows multiple expressions, or extra conditions, after a <code><span class='Head'>?</span></code>. It's not how I would have designed it if I just wanted to make a syntax for if statements, but it's a natural fit for the header system.</p>
|