aboutsummaryrefslogtreecommitdiff
path: root/docs/doc/block.html
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2021-08-11 17:21:31 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2021-08-11 17:25:04 -0400
commit2afb23928e1984d475cc460e1672e8f6fa0e4dbe (patch)
treeebd2cc514294d30b6fa4b36c2ee638326f06ef72 /docs/doc/block.html
parenteac61ca02074c218667754d5f4ef562e780bae85 (diff)
Allow clicking on header to get fragment link
Diffstat (limited to 'docs/doc/block.html')
-rw-r--r--docs/doc/block.html24
1 files changed, 12 insertions, 12 deletions
diff --git a/docs/doc/block.html b/docs/doc/block.html
index 82547a15..a3fa4397 100644
--- a/docs/doc/block.html
+++ b/docs/doc/block.html
@@ -4,7 +4,7 @@
<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">Blocks</h1>
+<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.</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>
@@ -17,7 +17,7 @@
<span class='Brace'>{</span> <span class='Value'>a</span><span class='Gets'>←</span><span class='String'>&quot;inner&quot;</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">Headerless blocks</h2>
+<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 think 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>
@@ -66,7 +66,7 @@
</tbody>
</table>
<p>Of these, <code><span class='Value'>𝕣</span></code> is sort of a &quot;more special&quot; 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, allowing 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>, something that can't be done with ordinary names.</p>
-<h3 id="arguments">Arguments</h3>
+<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. Either of these names will turn 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. Unlike in Dyalog APL's dfns, 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'>&quot;abcd&quot;</span>
⟨ 0 0 1 0 ⟩
@@ -97,7 +97,7 @@
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">Operands</h3>
+<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
@@ -116,7 +116,7 @@
⟨ ⟨ 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">Self-reference</h3>
+<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'>&lt;</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>
@@ -134,7 +134,7 @@
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">Block headers</h2>
+<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='Value'>:</span></code>, specifies the kind of block and can declare names for the block and its inputs. Its syntax mirrors an application of the block. As suggested by the positioning, the names given in a header apply only inside the block.</p>
<pre><span class='Comment'># A dyadic function called Func
</span><span class='Brace'>{</span> <span class='Value'>l</span> <span class='Function'>Func</span> <span class='Value'>r:</span>
@@ -160,16 +160,16 @@
<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">Destructuring</h3>
+<h3 id="destructuring"><a class="header" href="#destructuring">Destructuring</a></h3>
<p>Arguments, but not operands, allow destructuring 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+KfqGIsMuKfqTogYeKJjWIgfQpEZXN0cnVjdCAgICAgICA14oC/MeKAv+KfqDcsMuKfqQ==">↗️</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='Number'>2</span><span class='Bracket'>⟩</span><span class='Value'>:</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'>2</span><span class='Bracket'>⟩</span>
⟨ 5 7 ⟩
</pre>
-<h3 id="special-names-in-headers">Special names in headers</h3>
+<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></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">Short headers</h3>
+<h3 id="short-headers"><a class="header" href="#short-headers">Short headers</a></h3>
<p>A header does not need to include all inputs, as shown by the <code><span class='Function'>F</span> <span class='Modifier2'>_op_</span> <span class='Value'>val:</span></code> header above. The simplest case, when no inputs are given, is called a <em>label</em>. While it doesn't restrict the inputs, a label specifies the type of the block and gives an internal name that can be used to refer to it.</p>
<pre><span class='Brace'>{</span> <span class='Value'>b:</span> <span class='Comment'># Block
</span><span class='Brace'>{</span> <span class='Function'>π•Š</span><span class='Value'>:</span> <span class='Comment'># Function
@@ -177,7 +177,7 @@
</span><span class='Brace'>{</span> <span class='Modifier2'>_𝕣_</span><span class='Value'>:</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, this name can't be used, except for <a href="#returns">returns</a>: it doesn't make sense to refer to a value while it is still being computed!</p>
-<h2 id="multiple-bodies">Multiple bodies</h2>
+<h2 id="multiple-bodies"><a class="header" href="#multiple-bodies">Multiple bodies</a></h2>
<p>Blocks that define functions and deferred modifiers can include more than one body, separated by semicolons <code><span class='Value'>;</span></code>. The body used for a particular evaluation is chosen based on the arguments the the block. One special case applies when there are exactly two bodies either without headers or with labels only: in this case, the first applies when there is 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='Value'>;</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>
@@ -198,7 +198,7 @@
<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>
ERROR
</pre>
-<h3 id="case-headers">Case headers</h3>
+<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'>&quot;abc&quot;</span><span class='Value'>:</span> <span class='String'>&quot;string&quot;</span>
@@ -208,6 +208,6 @@ ERROR
<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>
-<h2 id="returns">Returns</h2>
+<h2 id="returns"><a class="header" href="#returns">Returns</a></h2>
<p><em>This feature is not yet included in any BQN implementation.</em></p>
<p>The glyph <code><span class='Gets'>β†’</span></code> indicates an early return from a block. It must be preceded either by one of the self-reference special names <code><span class='Function'>π•Š</span></code> or <code><span class='Value'>𝕣</span></code> or by an internal name for a containing block. The combination of name and return tokenβ€”like <code><span class='Function'>F</span><span class='Gets'>β†’</span></code>, let's sayβ€”is a function that returns from the current instance of the indicated block. If that instance has already returned, then it instead results in an error.</p>