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
|
<head>
<link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/>
<link href="../style.css" rel="stylesheet"/>
<title>BQN: Syntax overview</title>
</head>
<div class="nav"><a href="https://github.com/mlochbaum/BQN">BQN</a> / <a href="../index.html">main</a> / <a href="index.html">doc</a></div>
<h1 id="syntax-overview">Syntax overview</h1>
<p>BQN syntax consists of expressions where computation is done with a little organizing structure around them like assignment, functions, and list notation. Expressions are where the programmer is in control so the design tries to do as much as possible with them before introducing special syntax.</p>
<h2 id="special-glyphs">Special glyphs</h2>
<p>The following glyphs are used for BQN syntax. <a href="primitive.html">Primitives</a> (built-in functions and modifiers) are not listed in this table, and have their own page. Digits, characters, and the underscore <code><span class='Modifier2'>_</span></code> are used for numbers, and identifiers or variable names.</p>
<table>
<thead>
<tr>
<th>Glyph(s)</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Comment'>#</span></code></td>
<td><a href="#comments">Comment</a></td>
</tr>
<tr>
<td><code><span class='String'>'"</span></code></td>
<td><a href="#constants">Character or string literal</a></td>
</tr>
<tr>
<td><code><span class='String'>@</span></code></td>
<td><a href="#constants">Null character</a></td>
</tr>
<tr>
<td><code><span class='Number'>¯∞π</span></code></td>
<td><a href="#constants">Used in numeric literals</a></td>
</tr>
<tr>
<td><code><span class='Paren'>()</span></code></td>
<td>Expression grouping</td>
</tr>
<tr>
<td><code><span class='Gets'>←</span></code></td>
<td><a href="#assignment">Define</a></td>
</tr>
<tr>
<td><code><span class='Gets'>⇐</span></code></td>
<td><a href="#exports">Export</a></td>
</tr>
<tr>
<td><code><span class='Gets'>↩</span></code></td>
<td><a href="#assignment">Change</a></td>
</tr>
<tr>
<td><code><span class='Gets'>→</span></code></td>
<td><a href="block.html#returns">Return</a></td>
</tr>
<tr>
<td><code><span class='Separator'>⋄,</span></code> or newline</td>
<td>Statement or element <a href="#separators">separator</a></td>
</tr>
<tr>
<td><code><span class='Bracket'>⟨⟩</span></code></td>
<td><a href="#list-notation">List</a> (rank-1 array)</td>
</tr>
<tr>
<td><code><span class='Ligature'>‿</span></code></td>
<td><a href="#list-notation">Strand</a> (lightweight list syntax)</td>
</tr>
<tr>
<td><code><span class='Brace'>{}</span></code></td>
<td><a href="#blocks">Block</a> such as a function definition</td>
</tr>
<tr>
<td><code><span class='Value'>:</span></code></td>
<td><a href="block.html#block-headers">Block header</a></td>
</tr>
<tr>
<td><code><span class='Value'>;</span></code></td>
<td><a href="block.html#multiple-bodies">Block body separator</a></td>
</tr>
<tr>
<td><code><span class='Value'>𝕨</span><span class='Function'>𝕎</span></code></td>
<td><a href="#blocks">Left argument</a></td>
</tr>
<tr>
<td><code><span class='Value'>𝕩</span><span class='Function'>𝕏</span></code></td>
<td><a href="#blocks">Right argument</a></td>
</tr>
<tr>
<td><code><span class='Value'>𝕤</span><span class='Function'>𝕊</span></code></td>
<td><a href="#blocks">Function self-reference</a></td>
</tr>
<tr>
<td><code><span class='Value'>𝕗</span><span class='Function'>𝔽</span></code></td>
<td><a href="#blocks">Left operand of a modifier</a></td>
</tr>
<tr>
<td><code><span class='Value'>𝕘</span><span class='Function'>𝔾</span></code></td>
<td><a href="#blocks">Right operand of a 2-modifier</a></td>
</tr>
<tr>
<td><code><span class='Value'>𝕣</span></code></td>
<td><a href="#blocks">Modifier self-reference</a></td>
</tr>
</tbody>
</table>
<h2 id="comments">Comments</h2>
<p>A comment starts with <code><span class='Comment'>#</span></code> that is not part of a string and continues to the end of the line.</p>
<h2 id="constants">Constants</h2>
<p>BQN has single-token notation for numbers, strings, and characters.</p>
<p>Numbers allow the typical decimal notation with <code><span class='Number'>¯</span></code> for the negative sign (because <code><span class='Function'>-</span></code> is a function) and <code><span class='Value'>e</span></code> for scientific notation (or <code><span class='Function'>E</span></code>, as numeric notation is case-insensitive). <code><span class='Number'>∞</span></code> and <code><span class='Number'>π</span></code> may be used as special numeric values. If complex numbers are supported, then they can be written with the components separated by <code><span class='Value'>i</span></code>. However, no BQN to date supports complex numbers.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oIMKvz4Ag4ouEIDAuNSDii4QgNWXCrzEg4ouEIDEuNUUzIOKLhCDiiJ4g4p+pICAgIyBBIGxpc3Qgb2YgbnVtYmVycw==">↗️</a><pre> <span class='Bracket'>⟨</span> <span class='Number'>¯π</span> <span class='Separator'>⋄</span> <span class='Number'>0.5</span> <span class='Separator'>⋄</span> <span class='Number'>5e¯1</span> <span class='Separator'>⋄</span> <span class='Number'>1.5E3</span> <span class='Separator'>⋄</span> <span class='Number'>∞</span> <span class='Bracket'>⟩</span> <span class='Comment'># A list of numbers
</span>⟨ ¯3.141592653589793 0.5 0.5 1500 ∞ ⟩
</pre>
<p>Strings are written with double quotes <code><span class='String'>""</span></code>, and characters with single quotes <code><span class='String'>''</span></code> with a single character in between. A double quote within a string can be escaped by writing it twice; if two string literals are next to each other, they must be separated by a space. In contrast, character literals do not use escapes, as the length is already known.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omgwqgg4p+oICJzdHIiIOKLhCAicyd0IiJyIiDii4QgJ2MnIOKLhCAnJycg4ouEICciJyDin6kgICAjICIiIGlzIGFuIGVzY2FwZQoK4omhwqgg4p+oICJhIiDii4QgJ2EnIOKfqSAgICMgQSBzdHJpbmcgaXMgYW4gYXJyYXkgYnV0IGEgY2hhcmFjdGVyIGlzbid0">↗️</a><pre> <span class='Function'>≠</span><span class='Modifier'>¨</span> <span class='Bracket'>⟨</span> <span class='String'>"str"</span> <span class='Separator'>⋄</span> <span class='String'>"s't""r"</span> <span class='Separator'>⋄</span> <span class='String'>'c'</span> <span class='Separator'>⋄</span> <span class='String'>'''</span> <span class='Separator'>⋄</span> <span class='String'>'"'</span> <span class='Bracket'>⟩</span> <span class='Comment'># "" is an escape
</span>⟨ 3 5 1 1 1 ⟩
<span class='Function'>≡</span><span class='Modifier'>¨</span> <span class='Bracket'>⟨</span> <span class='String'>"a"</span> <span class='Separator'>⋄</span> <span class='String'>'a'</span> <span class='Bracket'>⟩</span> <span class='Comment'># A string is an array but a character isn't
</span>⟨ 1 0 ⟩
</pre>
<p>The null character (code point 0) has a dedicated literal representation <code><span class='String'>@</span></code>. This character can be used to directly convert between characters and numeric code points, which among many other uses allows tricky characters to be entered by code point: for example, a non-breaking space is <code><span class='String'>@</span><span class='Function'>+</span><span class='Number'>160</span></code>. The character can also be entered as a character literal, but this will display differently in various editors and some tools may have trouble with a file directly containing a null, so it is best to use <code><span class='String'>@</span></code> instead.</p>
<h2 id="expressions">Expressions</h2>
<p><em><a href="context.html">More discussion</a></em></p>
<p>Like APL, BQN uses four <em>syntactic roles</em> for values in expressions:</p>
<ul>
<li><strong>Subjects</strong>, like APL arrays or J nouns</li>
<li><strong>Functions</strong>, or verbs in J</li>
<li><strong>1-Modifiers</strong>, like APL monadic operators or J adverbs</li>
<li><strong>2-Modifiers</strong>, like APL dyadic operators or J conjunctions.</li>
</ul>
<p>These roles work exactly like they do in APL, with functions applying to one or two subject arguments, 1-modifiers taking a single function or subject on the left, and 2-modifiers taking a function or subject on each side.</p>
<p>Unlike APL, in BQN the syntactic role of an identifier is determined purely by the way it's spelled: a lowercase first letter (<code><span class='Value'>name</span></code>) makes it a subject, an uppercase first letter (<code><span class='Function'>Name</span></code>) makes it a function, and underscores are used for 1-modifiers (<code><span class='Modifier'>_name</span></code>) and 2-modifiers (<code><span class='Modifier2'>_name_</span></code>). Below, the function <code><span class='Brace'>{</span><span class='Function'>𝕎</span><span class='Value'>𝕩</span><span class='Brace'>}</span></code> treats its left argument <code><span class='Function'>𝕎</span></code> as a function and its right argument <code><span class='Value'>𝕩</span></code> as a subject. With a list of functions, we can make a table of the square and square root of a few numbers:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+ow5fLnCziiJrin6kge/CdlY7wnZWpfeKMnCAx4oC/NOKAvzk=">↗️</a><pre> <span class='Bracket'>⟨</span><span class='Function'>×</span><span class='Modifier'>˜</span><span class='Separator'>,</span><span class='Function'>√</span><span class='Bracket'>⟩</span> <span class='Brace'>{</span><span class='Function'>𝕎</span><span class='Value'>𝕩</span><span class='Brace'>}</span><span class='Modifier'>⌜</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>9</span>
┌─
╵ 1 16 81
1 2 3
┘
</pre>
<p>BQN's built-in operations also have patterns to indicate the syntactic role: 1-modifiers (<code><span class='Modifier'>˜¨˘⁼⌜´`</span></code>) are all superscript characters, and 2-modifiers (<code><span class='Modifier2'>∘○⊸⟜⌾⊘◶⚇⎉⍟</span></code>) all have an unbroken circle (two functions <code><span class='Function'>⌽⍉</span></code> have broken circles with lines through them). Every other built-in constant is a function, although the special symbols <code><span class='Number'>¯</span></code>, <code><span class='Number'>∞</span></code>, and <code><span class='Number'>π</span></code> are used as part of numeric literal notation.</p>
<h3 id="assignment">Assignment</h3>
<p>Another element that can be included in expressions is assignment, which is written with <code><span class='Gets'>←</span></code> to <em>define</em> (also called "declare" in many other languages) a variable and <code><span class='Gets'>↩</span></code> to <em>change</em> its definition. A variable can only be defined once within a scope, and can only be changed if it has already been defined. However, it can be shadowed, meaning that it is defined again in an inner scope even though it has a definition in an outer scope already.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eOKGkDEg4ouEIHt44oaQMiDii4QgeOKGqTMg4ouEIHh9Cng=">↗️</a><pre> <span class='Value'>x</span><span class='Gets'>←</span><span class='Number'>1</span> <span class='Separator'>⋄</span> <span class='Brace'>{</span><span class='Value'>x</span><span class='Gets'>←</span><span class='Number'>2</span> <span class='Separator'>⋄</span> <span class='Value'>x</span><span class='Gets'>↩</span><span class='Number'>3</span> <span class='Separator'>⋄</span> <span class='Value'>x</span><span class='Brace'>}</span>
3
<span class='Value'>x</span>
1
</pre>
<p>Assignment can be used inline in an expression, and its result is always the value being assigned. The role of the identifier used must match the value being assigned.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MsOXYeKGkChOZWfihpAtKTMKYQ==">↗️</a><pre> <span class='Number'>2</span><span class='Function'>×</span><span class='Value'>a</span><span class='Gets'>←</span><span class='Paren'>(</span><span class='Function'>Neg</span><span class='Gets'>←</span><span class='Function'>-</span><span class='Paren'>)</span><span class='Number'>3</span>
¯6
<span class='Value'>a</span>
¯3
</pre>
<h3 id="exports">Exports</h3>
<p>See <a href="namespace.html">namespaces</a>: this section is a little out of date.</p>
<p>The double arrow <code><span class='Gets'>⇐</span></code> is used to export variables from an immediate block or file. It can only be used in these contexts, and not in function or modifier blocks. There are two ways to export variables. First, <code><span class='Gets'>←</span></code> in the variable definition can be replaced with <code><span class='Gets'>⇐</span></code> to export the variable as it's defined. Second, an export statement consisting of an assignment target followed by <code><span class='Gets'>⇐</span></code> with nothing to the right exports the variables in the assignment target and does nothing else. Export statements can be placed anywhere in the relevant program or body, including before declaration or on the last line, and a given variable can be exported any number of times.</p>
<pre><span class='Bracket'>⟨</span><span class='Value'>alias</span><span class='Gets'>⇐</span><span class='Value'>a</span><span class='Separator'>,</span> <span class='Value'>b</span><span class='Separator'>,</span> <span class='Value'>c0</span><span class='Ligature'>‿</span><span class='Value'>c1</span><span class='Gets'>⇐</span><span class='Value'>c</span><span class='Separator'>,</span> <span class='Value'>b2</span><span class='Gets'>⇐</span><span class='Value'>b</span><span class='Bracket'>⟩</span><span class='Gets'>←</span><span class='Brace'>{</span>
<span class='Value'>b</span><span class='Ligature'>‿</span><span class='Value'>c</span><span class='Gets'>⇐</span> <span class='Comment'># Non-definition exports can go anywhere
</span> <span class='Value'>a</span><span class='Gets'>⇐</span><span class='Number'>2</span> <span class='Comment'># Define and export
</span> <span class='Value'>b</span><span class='Gets'>←</span><span class='Number'>1</span><span class='Function'>+</span><span class='Value'>a</span>
<span class='Value'>c</span><span class='Gets'>←</span><span class='Value'>b</span><span class='Ligature'>‿</span><span class='String'>"str"</span>
<span class='Brace'>}</span>
</pre>
<p>A block with exports is a <em>namespace block</em>, and in versions of BQN without first-class namespaces it can only be used as part of a destructuring assignment. This assignment's target is a list where each element specifies one of the names exported by the block and what it should be assigned to. The element can be either a single name (such as <code><span class='Value'>b</span></code> above), which gives both, or a combination of the assignment target, then <code><span class='Gets'>⇐</span></code>, then a name. If <code><span class='Gets'>⇐</span></code> is never used, the names can be given as a strand with <code><span class='Ligature'>‿</span></code>. To use <code><span class='Gets'>⇐</span></code> for aliases, bracket syntax <code><span class='Bracket'>⟨⟩</span></code> is needed. Imported names can be repeated and can be spelled with any role (the role is ignored).</p>
<h2 id="lists-and-blocks">Lists and blocks</h2>
<h3 id="separators">Separators</h3>
<p>The characters <code><span class='Separator'>⋄</span></code> and <code><span class='Separator'>,</span></code> and newline are completely interchangeable and are used to separate expressions. An expression might be an element in a list or a line in a function. Empty sections—those that consist only of whitespace—are ignored. This means that any number of separators can be used between expressions, and that leading and trailing separators are also allowed. The expressions are evaluated in text order: left to right and top to bottom.</p>
<h3 id="list-notation">List notation</h3>
<p>Lists (1-dimensional arrays) are enclosed in angle brackets <code><span class='Bracket'>⟨⟩</span></code>, with the results of the expressions in between being the list's elements. Lists of two elements or more can also be written with the ligature character <code><span class='Ligature'>‿</span></code>. This character has higher binding strength than any part of an expression. If one of the elements is a compound expression, then it will need to be enclosed in parentheses.</p>
<h3 id="blocks">Blocks</h3>
<p><em><a href="block.html">Full documentation</a></em></p>
<p>Blocks are written with curly braces <code><span class='Brace'>{}</span></code> and can be used to group expressions or define functions and modifiers. The contents are simply a sequence of expressions, where each is evaluated and the result of the last is returned in order to evaluate the block. This result can have any value, and its syntactic role in the calling context is determined by the normal rules: functions return subjects and modifiers return functions. Blocks have lexical scope.</p>
<p>The special names <code><span class='Value'>𝕨</span></code> and <code><span class='Value'>𝕩</span></code>, which stand for arguments, and <code><span class='Value'>𝕗</span></code> and <code><span class='Value'>𝕘</span></code>, which stand for operands, are available inside curly braces. Like ordinary names, the lowercase forms indicate subjects and the uppercase forms <code><span class='Function'>𝕎𝕏𝔽𝔾</span></code> indicate functions. The type and syntactic role of the block is determined by its contents: a 2-modifier contains <code><span class='Value'>𝕘</span></code>, a 1-modifier contains <code><span class='Value'>𝕗</span></code> but not <code><span class='Value'>𝕘</span></code>, and a function contains neither but does have one of <code><span class='Value'>𝕨𝕩𝕤</span><span class='Function'>𝕎𝕏𝕊</span></code>. If no special names are present the block is an <em>immediate block</em> and is evaluated as soon as it appears, with the result having a subject role.</p>
<p>A modifier can be evaluated twice: once when passed operands and again when the resulting function is passed arguments. If it contains <code><span class='Value'>𝕨</span></code> or <code><span class='Value'>𝕩</span></code>, the first evaluation simply remembers the operands, and the contents will be executed only on the second evaluation, when the arguments are available. If it doesn't contain these, then the contents are executed on the first evaluation and the result is treated as a function.</p>
|