diff options
Diffstat (limited to 'docs/doc/control.html')
| -rw-r--r-- | docs/doc/control.html | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/docs/doc/control.html b/docs/doc/control.html index ce33ddb6..10a9fc7a 100644 --- a/docs/doc/control.html +++ b/docs/doc/control.html @@ -4,7 +4,7 @@ <title>Control flow in BQN</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="control-flow-in-bqn">Control flow in BQN</h1> +<h1 id="control-flow-in-bqn"><a class="header" href="#control-flow-in-bqn">Control flow in BQN</a></h1> <p>BQN does not have ALGOL-style control structures. Instead, functional techniques can be used to control when code is evaluated. This page describes how BQN functionality can be used to emulate something more familiar to an imperative programmer.</p> <p>Control structures here are always functions that act on lists of functions, although alternatives might be presented. This is because stranded functions can be formatted in a very similar way to blocks in curly-brace languages. However, there are many ways to write control flow, including simple operators and a mix of operators and more control-structure-like code. Implementing a control structure rarely takes much code with any method, so there are usually several simple ways to implement a given flow or a variation of it.</p> <p>The surfeit of ways to write control structures could be a bit of an issue for reading BQN. My hope is that the community can eventually settle on a smaller set of standard forms to recommend so that you won't have to recognize all the variants given here. On the other hand, the cost of using specialized control structures is lower in a large project without too many contributors. In this case BQN's flexibility allows developers to adapt to the project's particular demands (for example, some programs use switch/case statements heavily but most do not).</p> @@ -21,7 +21,7 @@ <span class='Function'>Switch</span> <span class='Gets'>β</span> <span class='Brace'>{</span><span class='Value'>c</span><span class='Gets'>β</span><span class='Function'>β</span><span class='Value'>π©</span> <span class='Separator'>β</span> <span class='Value'>m</span><span class='Ligature'>βΏ</span><span class='Value'>a</span><span class='Gets'>β</span><span class='Function'><</span><span class='Modifier'>Λ</span><span class='Function'>β</span><span class='Modifier2'>β</span><span class='Ligature'>βΏ</span><span class='Number'>2</span><span class='Function'>β₯</span><span class='Number'>1</span><span class='Function'>β</span><span class='Value'>π©</span> <span class='Separator'>β</span> <span class='Paren'>(</span><span class='Function'>β</span><span class='Value'>a</span><span class='Function'>βC</span><span class='Paren'>)</span><span class='Modifier2'>βΆ</span><span class='Value'>m</span><span class='String'>@</span><span class='Brace'>}</span> <span class='Function'>Test</span> <span class='Gets'>β</span> <span class='Brace'>{</span><span class='Value'>fn</span><span class='Gets'>β</span><span class='Brace'>{</span><span class='Function'>C</span><span class='Ligature'>βΏ</span><span class='Function'>Aπ</span><span class='Value'>e:</span><span class='Function'>C</span><span class='Modifier2'>βΆ</span><span class='Function'>A</span><span class='Ligature'>βΏ</span><span class='Function'>E</span><span class='Brace'>}</span><span class='Modifier'>Β΄</span><span class='Value'>π©</span><span class='Separator'>β</span><span class='Function'>Fn</span><span class='String'>@</span><span class='Brace'>}</span> </pre> -<h2 id="blocks-and-functions">Blocks and functions</h2> +<h2 id="blocks-and-functions"><a class="header" href="#blocks-and-functions">Blocks and functions</a></h2> <p>Control structures are generally defined to work with blocks of code, which they might skip, or execute one or more times. This might sound like a BQN immediate block, which also consists of a sequence of code to execute, but immediate blocks are always executed as soon as they are encountered and can't be manipulated the way that blocks in imperative languages can. They're intended to be used with <a href="lexical.html">lexical scoping</a> as a tool for encapsulation. Instead, the main tool we will use to get control structures is the block function.</p> <p>Using functions as blocks is a little outside their intended purpose, and the fact that they have to be passed an argument and are expected to use it will be a minor annoyance. The following conventions signal a function that ignores its argument and is called purely for the side effects:</p> <ul> @@ -33,7 +33,7 @@ <span class='Function'>Fn</span> <span class='Gets'>β</span> <span class='Brace'>{</span><span class='Value'>π€</span><span class='Separator'>β</span> <span class='Value'>m</span><span class='Function'>+</span><span class='Gets'>β©</span><span class='Number'>1</span><span class='Separator'>,</span><span class='Value'>n</span><span class='Function'>Γ</span><span class='Gets'>β©</span><span class='Number'>2</span><span class='Brace'>}</span><span class='Separator'>,</span> <span class='Function'>Fn</span> <span class='String'>@</span> </pre> <p>Control structures are called "statements" below to match common usage, but they are actually expressions, and return a value that might be used later.</p> -<h2 id="if">If</h2> +<h2 id="if"><a class="header" href="#if">If</a></h2> <p>The if statement conditionally performs some action. It is similar to the Repeat (<code><span class='Modifier2'>β</span></code>) modifier with a right operand returning a boolean: <code><span class='Function'>Fn</span><span class='Modifier2'>β</span><span class='Function'>Cond</span> <span class='Value'>π©</span></code> gives <code><span class='Function'>Fn</span> <span class='Value'>π©</span></code> if <code><span class='Function'>Cond</span> <span class='Value'>π©</span></code> is <code><span class='Number'>1</span></code>, and returns <code><span class='Value'>π©</span></code> without calling <code><span class='Function'>Fn</span></code> if <code><span class='Function'>Cond</span> <span class='Value'>π©</span></code> is <code><span class='Number'>0</span></code>. Here is how we might make it behave like a control structure.</p> <pre><span class='Brace'>{</span><span class='Value'>π€</span><span class='Separator'>β</span><span class='Value'>a</span><span class='Function'>+</span><span class='Gets'>β©</span><span class='Number'>10</span><span class='Brace'>}</span><span class='Modifier2'>β</span><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'><</span><span class='Number'>10</span><span class='Paren'>)</span> <span class='String'>@</span> </pre> @@ -54,10 +54,10 @@ <span class='Brace'>}</span> </pre> <p>In all cases, the result of an if statement is the result of the action if it's performed, and otherwise it's whatever argument was passed to the statement, which is <code><span class='String'>@</span></code> in most examples above.</p> -<h2 id="repeat">Repeat</h2> +<h2 id="repeat"><a class="header" href="#repeat">Repeat</a></h2> <p>There's no reason the condition in an if statement from the previous section has to be boolean: it could be any natural number, causing the action to be repeated that many times. If the action is never performed, the result is the statement's argument, and otherwise it's the result of the last time the action was performed.</p> <p>Another option is to use a <a href="#for">for-each</a> statement with an argument of <code><span class='Function'>β</span><span class='Value'>n</span></code>: in this case the result is the list of each action's result.</p> -<h2 id="if-else">If-Else</h2> +<h2 id="if-else"><a class="header" href="#if-else">If-Else</a></h2> <p>Despite the name, an if-else statement is most closely related to a <a href="#switch-case">switch-case</a> statement: in fact, it's just a special case where the two cases are true (<code><span class='Number'>1</span></code>) and false (<code><span class='Number'>0</span></code>). As a result, we can implement it either with Choose (<code><span class='Modifier2'>βΆ</span></code>) or with <a href="block.html#case-headers">case headers</a> of <code><span class='Number'>1</span></code> and <code><span class='Number'>0</span></code>.</p> <p>When using Choose, note that the natural ordering places the false case before the true one to match list index ordering. To get the typical if-else order, the condition should be negated or the statements reversed. Here's a function to get an if-else statement by swapping the conditions, and two ways its application might be written.</p> <pre><span class='Function'>IfElse</span> <span class='Gets'>β</span> <span class='Brace'>{</span><span class='Value'>cond</span><span class='Ligature'>βΏ</span><span class='Function'>True</span><span class='Ligature'>βΏ</span><span class='Function'>False</span><span class='Value'>:</span> <span class='Value'>cond</span><span class='Modifier2'>βΆ</span><span class='Function'>False</span><span class='Ligature'>βΏ</span><span class='Function'>True</span> <span class='String'>@</span><span class='Brace'>}</span> @@ -81,7 +81,7 @@ <span class='Brace'>}</span> </pre> <p>The result of an if-else statement is just the result of whichever branch was used; chained if-else and switch-case statements will work the same way.</p> -<h3 id="chained-if-else">Chained If-Else</h3> +<h3 id="chained-if-else"><a class="header" href="#chained-if-else">Chained If-Else</a></h3> <p>One pattern in imperative languages is to check one condition and apply an action if it succeeds, but check a different condition if it fails, in sequence until some condition succeeds or every one has been checked. Languages might make this pattern easier by making if-else right associative, so that the programmer can write an <code><span class='Value'>if</span></code> statement followed by a sequence of <code><span class='Value'>else</span> <span class='Value'>if</span></code> "statements", or might just provide a unified <code><span class='Value'>elif</span></code> keyword that works similarly (while this <em>is</em> a common pattern, I suspect it's used more often than it's really wanted because of this syntactic support).</p> <p>In BQN it's possible to nest <code><span class='Function'>IfElse</span></code> expressions, but it's also possible to write a control structure that chains them all at one level. For this statement the input will be a sequence of <code><span class='Bracket'>β¨</span><span class='Function'>Test</span><span class='Separator'>,</span><span class='Function'>Action</span><span class='Bracket'>β©</span></code> pairs, followed by a final action to perform if no test succeeds. The first test is always performed; other tests should be wrapped in blocks because otherwise they'll be executed even if an earlier test succeeded.</p> <pre><span class='Function'>Test</span> <span class='Gets'>β</span> <span class='Brace'>{</span><span class='Value'>fn</span><span class='Gets'>β</span><span class='Brace'>{</span><span class='Function'>Cond</span><span class='Ligature'>βΏ</span><span class='Function'>Act</span> <span class='Function'>π</span> <span class='Value'>else:</span> <span class='Function'>Cond</span><span class='Modifier2'>βΆ</span><span class='Function'>Else</span><span class='Ligature'>βΏ</span><span class='Function'>Act</span><span class='Brace'>}</span><span class='Modifier'>Β΄</span><span class='Value'>π©</span> <span class='Separator'>β</span> <span class='Function'>Fn</span><span class='String'>@</span><span class='Brace'>}</span> @@ -92,7 +92,7 @@ <span class='Brace'>{</span><span class='Value'>π€</span><span class='Separator'>β</span><span class='Value'>a</span><span class='Function'>-</span><span class='Gets'>β©</span><span class='Number'>2</span><span class='Brace'>}</span> <span class='Bracket'>β©</span> </pre> -<h2 id="switch-case">Switch-Case</h2> +<h2 id="switch-case"><a class="header" href="#switch-case">Switch-Case</a></h2> <p>The simplest way to write a switch-case statement is with <a href="block.html#case-headers">case headers</a> in a monadic function. A function with case headers tests its input against the headers in order until one matches, then executes the code there. To make it into a control structure, we just want to call the function on a given value.</p> <pre><span class='Function'>Match</span> <span class='Gets'>β</span> <span class='Brace'>{</span><span class='Function'>π</span><span class='Value'>π¨</span><span class='Brace'>}</span><span class='Modifier'>Β΄</span> @@ -126,7 +126,7 @@ <span class='Bracket'>β©</span> </pre> <p>Finally, the most general form of a switch statement is a <a href="#chained-if-else">chained if-else</a>!</p> -<h2 id="loop-forever">Loop forever</h2> +<h2 id="loop-forever"><a class="header" href="#loop-forever">Loop forever</a></h2> <p>It's not a particularly common pattern, but this is a good simple case to warm up for the while loop. BQN primitives usually take a predictable amount of time, and none of them will run forever! Recursion is the tool to use here. If there's a particular function that we'd like to run infinity times, we can just add <code><span class='Value'>π¨</span><span class='Function'>π</span><span class='Value'>π©</span></code> to the end:</p> <pre><span class='Brace'>{</span> <span class='Comment'># Stuff to do forever @@ -148,7 +148,7 @@ <span class='Comment'># Stuff to do forever </span><span class='Brace'>}</span> </pre> -<h2 id="while">While</h2> +<h2 id="while"><a class="header" href="#while">While</a></h2> <p>The same modifier technique used in <code><span class='Function'>Forever</span></code> works for a while loop as well. Because there are now two componentsβthe condition and actionβwe'll use a 2-modifier instead of a 1-modifier.</p> <pre><span class='Function'>While</span> <span class='Gets'>β</span> <span class='Brace'>{</span><span class='Value'>π¨</span><span class='Brace'>{</span><span class='Function'>π</span><span class='Modifier2'>β</span><span class='Function'>πΎ</span><span class='Modifier2'>β</span><span class='Function'>π½</span><span class='Value'>π©</span><span class='Brace'>}</span><span class='Value'>π©</span><span class='String'>@</span><span class='Brace'>}</span><span class='Modifier'>Β΄</span> @@ -160,12 +160,12 @@ <pre><span class='Function'>DoWhile</span> <span class='Gets'>β</span> <span class='Brace'>{</span><span class='Value'>π¨</span><span class='Brace'>{</span><span class='Function'>π</span><span class='Modifier2'>β</span><span class='Function'>π½πΎ</span><span class='Value'>π©</span><span class='Brace'>}</span><span class='Value'>π©</span><span class='String'>@</span><span class='Brace'>}</span><span class='Modifier'>Β΄</span> </pre> <p>Because the condition is run repeatedly, it has to be a function, and can't be a plain expression as in an if conditional.</p> -<h3 id="low-stack-version">Low-stack version</h3> +<h3 id="low-stack-version"><a class="header" href="#low-stack-version">Low-stack version</a></h3> <p>The above version of <code><span class='Function'>While</span></code> will fail in a fairly small number of iterations, because it consumes a new stack frame with each iteration. While tail call optimization could solve this, detecting the tail call in a compound function like <code><span class='Function'>π</span><span class='Modifier2'>β</span><span class='Function'>πΎ</span><span class='Modifier2'>β</span><span class='Function'>π½</span></code> is technically difficult and would introduce overhead into a BQN interpreter. However, there is a method to make the number of required stack frames logarithmic in the number of iterations instead of linear:</p> <pre><span class='Function'>While</span> <span class='Gets'>β</span> <span class='Brace'>{</span><span class='Value'>π©</span><span class='Brace'>{</span><span class='Function'>π½</span><span class='Modifier2'>β</span><span class='Function'>πΎ</span><span class='Modifier2'>β</span><span class='Function'>π½</span><span class='Modifier2'>_π£_</span><span class='Function'>πΎ</span><span class='Modifier2'>β</span><span class='Function'>π½</span><span class='Modifier2'>β</span><span class='Function'>πΎ</span><span class='Value'>π©</span><span class='Brace'>}</span><span class='Value'>π¨</span><span class='String'>@</span><span class='Brace'>}</span><span class='Modifier'>Β΄</span> </pre> <p>The innovation is to use <code><span class='Brace'>{</span><span class='Function'>π½</span><span class='Modifier2'>β</span><span class='Function'>πΎ</span><span class='Modifier2'>β</span><span class='Function'>π½</span><span class='Modifier2'>_π£_</span><span class='Function'>πΎ</span><span class='Modifier2'>β</span><span class='Function'>π½</span><span class='Modifier2'>β</span><span class='Function'>πΎ</span><span class='Value'>π©</span><span class='Brace'>}</span></code> instead of the equivalent <code><span class='Brace'>{</span><span class='Function'>π½</span><span class='Modifier2'>_π£_</span><span class='Function'>πΎ</span><span class='Modifier2'>β</span><span class='Function'>π½</span><span class='Modifier2'>β</span><span class='Function'>πΎ</span><span class='Value'>π©</span><span class='Brace'>}</span></code> or <code><span class='Brace'>{</span><span class='Function'>π</span><span class='Modifier2'>β</span><span class='Function'>π½</span><span class='Modifier2'>β</span><span class='Function'>πΎ</span><span class='Value'>π©</span><span class='Brace'>}</span></code> (these are the same, as <code><span class='Function'>π</span></code> in a modifier is defined as <code><span class='Function'>π½</span><span class='Modifier2'>_π£_</span><span class='Function'>πΎ</span></code>). Here <code><span class='Function'>π½</span></code> performs one iteration and <code><span class='Function'>πΎ</span></code> tests whether to continue. The simplest approach is to perform one iteration and recurse with the same two functions. The modified approach replaces <code><span class='Function'>π½</span></code> with <code><span class='Function'>π½</span><span class='Modifier2'>β</span><span class='Function'>πΎ</span><span class='Modifier2'>β</span><span class='Function'>π½</span></code>, that is, it doubles it while making sure the condition is still checked each iteration. The doublings compound so that recursion level <code><span class='Value'>n</span></code> performs <code><span class='Function'>π½</span></code> up to <code><span class='Number'>2</span><span class='Function'>β</span><span class='Value'>n</span></code> times while using on the order of <code><span class='Value'>n</span></code> additional stack frames. Only a hundred or two stack frames are needed to give a practically unlimited number of iterations.</p> -<h2 id="for">For</h2> +<h2 id="for"><a class="header" href="#for">For</a></h2> <p>To begin with, are you sure you don't want a for-each loop instead? In BQN that's just a function with Each (<code><span class='Modifier'>Β¨</span></code>), and it covers most common uses of a for loop.</p> <pre><span class='Function'>Fn</span><span class='Modifier'>Β¨</span> <span class='Value'>v</span> <span class='Comment'># for (π© in v) </span><span class='Function'>Fn</span><span class='Modifier'>Β¨</span> <span class='Function'>β</span><span class='Value'>n</span> <span class='Comment'># for (π©=0; π©<n; π©++) @@ -195,7 +195,7 @@ <span class='Brace'>}</span> <span class='Brace'>}</span><span class='Bracket'>β©</span> </pre> -<h3 id="break-and-continue">Break and continue</h3> +<h3 id="break-and-continue"><a class="header" href="#break-and-continue">Break and continue</a></h3> <p>In a <code><span class='Function'>While</span></code> or <code><span class='Function'>For</span></code> loop, <a href="block.html#returns">returns</a> can be used for either the break or the continue statement (or, for that matter, a multiline break) if available. Returning from the main body, either with <code><span class='Function'>π</span><span class='Gets'>β</span></code> or a labelled return, is a functional version of a continue statement. To escape from the loop as a whole, it should be wrapped in a labelled immediate block. Returning from that block using its label breaks the loop. For example, the following loop</p> <pre><span class='Brace'>{</span><span class='Value'>brk:</span> <span class='Value'>sum</span> <span class='Gets'>β</span> <span class='Number'>0</span> <span class='Separator'>β</span> <span class='Value'>even</span> <span class='Gets'>β</span> <span class='Bracket'>β¨β©</span> |
