diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-10-26 15:46:38 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-10-26 15:46:38 -0400 |
| commit | 1d6a9cf1441bd6d478977715d82031e77c20ce5c (patch) | |
| tree | 6fee5b999964c813594e9a5562b9e588d16d8536 /docs/doc/block.html | |
| parent | dd04717b7de252c300d197e5dcfaf9cab33d3ab5 (diff) | |
Don't include &run in documentation REPL links: it's no longer used
Diffstat (limited to 'docs/doc/block.html')
| -rw-r--r-- | docs/doc/block.html | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/docs/doc/block.html b/docs/doc/block.html index 24857cfe..21354448 100644 --- a/docs/doc/block.html +++ b/docs/doc/block.html @@ -7,19 +7,19 @@ <h1 id="blocks">Blocks</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=&run">โ๏ธ</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> +<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="https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scoping">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=&run">โ๏ธ</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> +<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">Headerless blocks</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=&run">โ๏ธ</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> +<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> @@ -68,7 +68,7 @@ <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, 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> <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+KGqTIg4ouEIDDiiL7wnZWpIH0gMwo0IHsg4p+o8J2VqeKLhC3wnZWo4p+pIH0gNQ==&run">โ๏ธ</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> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eydjJz3wnZWpfSAiYWJjZCIKeyDwnZWpK+KGqTIg4ouEIDDiiL7wnZWpIH0gMwo0IHsg4p+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 โฉ @@ -76,13 +76,13 @@ โจ 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 Nothing <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&run">โ๏ธ</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> +<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==&run">โ๏ธ</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> +<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" @@ -93,24 +93,24 @@ โ </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=&run">โ๏ธ</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> +<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.413159102577 </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> <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==&run">โ๏ธ</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> +<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&run">โ๏ธ</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> +<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&run">โ๏ธ</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> +<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 โฉ @@ -118,18 +118,18 @@ <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> <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==&run">โ๏ธ</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> +<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&run">โ๏ธ</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> +<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+oMSwgKPCdlajDl/CdlakpX/CdlaPin6kg8J2VqS0xIH0KRmFjdF9tb2QgNw==&run">โ๏ธ</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='Number'>1</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> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RmFjdF9tb2Qg4oaQIDEgeyAoMOKKuDwp4pe24p+oMSwgKPCdlajDl/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='Number'>1</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> 1 </pre> @@ -162,7 +162,7 @@ <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> <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==&run">โ๏ธ</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> +<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> @@ -179,14 +179,14 @@ <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> <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==&run">โ๏ธ</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> +<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> โจ 1 'a' โฉ <span class='String'>'a'</span> <span class='Function'>Ambiv</span> <span class='String'>'b'</span> โจ 2 'a' 'b' โฉ </pre> <p>Bodies before the last two must have headers that include arguments. When a block that includes this type of header is called, its headers are checked in order for compatibility with the arguments. 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&run">โ๏ธ</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='Value'>:</span><span class='Number'>0</span><span class='Ligature'>โฟ</span><span class='Number'>5</span> <span class='Value'>;</span> <span class='Number'>2</span><span class='Function'>๐</span><span class='Value'>๐ฉ:</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='Value'>;</span> <span class='Function'>๐</span><span class='Value'>๐ฉ:</span><span class='Number'>2</span><span class='Ligature'>โฟ</span><span class='Value'>๐ฉ</span> <span class='Brace'>}</span> +<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='Value'>:</span><span class='Number'>0</span><span class='Ligature'>โฟ</span><span class='Number'>5</span> <span class='Value'>;</span> <span class='Number'>2</span><span class='Function'>๐</span><span class='Value'>๐ฉ:</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='Value'>;</span> <span class='Function'>๐</span><span class='Value'>๐ฉ:</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> |
