aboutsummaryrefslogtreecommitdiff
path: root/doc/control.md
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2022-06-27 22:00:55 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2022-06-27 22:00:55 -0400
commit8389e763344637c01d0d7161091e5f2cd9b14251 (patch)
tree5406113f6711c6b1222650228cba453c2e4641fa /doc/control.md
parentb6185d5029e2adcc721c0cc2097f591d9a09f135 (diff)
Yet still more editing
Diffstat (limited to 'doc/control.md')
-rw-r--r--doc/control.md38
1 files changed, 19 insertions, 19 deletions
diff --git a/doc/control.md b/doc/control.md
index fe2e77e9..dcded65e 100644
--- a/doc/control.md
+++ b/doc/control.md
@@ -2,13 +2,13 @@
# Control flow in BQN
-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.
+BQN does not have ALGOL-style control structures. Instead, [functional](functional.md) 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.
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.
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).
-The useful control structures introduced here are collected as shortened definitions below. `While` uses the slightly more complicated implementation that avoids stack overflow, and `DoWhile` and `For` are written in terms of it in order to share this property. The more direct versions with linear stack use appear in the main text.
+The most useful control structures introduced here are collected as shortened definitions below. `While` uses the slightly more complicated implementation that avoids stack overflow, and `DoWhile` and `For` are written in terms of it in order to share this property. The more direct versions with linear stack use appear in the main text.
If ← {π•βŸπ•Ž@}Β΄ # Also Repeat
IfElse ← {cβ€ΏTβ€ΏF: cβ—ΆFβ€ΏT@}
@@ -19,18 +19,18 @@ The useful control structures introduced here are collected as shortened definit
# Switch/case statements have many variations; these are a few
Match ← {𝕏𝕨}Β΄
Select ← {(βŠ‘π•©)β—Ά(1↓𝕩)@}
- Switch ← {cβ†βŠ‘π•© β‹„ mβ€Ώa←<Λ˜β‰βˆ˜β€Ώ2β₯Š1↓𝕩 β‹„ (m⊸⊐⌾<C)β—Άa@}
+ Switch ← {cβ†βŠ‘π•© β‹„ [m,a]β†β‰βˆ˜β€Ώ2β₯Š1↓𝕩 β‹„ (m⊸⊐⌾<C)β—Άa@}
Test ← {fn←{Cβ€ΏAπ•Še:Cβ—ΆAβ€ΏE}´𝕩⋄Fn@}
## Blocks and functions
-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 [lexical scoping](lexical.md) as a tool for encapsulation. Instead, the main tool we will use to get control structures is the block function.
+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](block.md), 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 [lexical scoping](lexical.md) as a tool for encapsulation. Instead, the main tool we will use to get control structures is the block function.
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:
- Pass `@` to a function that ignores its argument. It's a nice signal that nothing is happening and is easy to type.
- A headerless function that doesn't use an argument will be interpreted as an immediate block by default. Start it with the line `𝕀` to avoid this (it's an instruction to navel gaze: the function contemplates its self, but does nothing about it). Other options like `π•Š:`, `F:`, or `𝕩` also work, but are more visually distracting.
-Even with these workarounds, BQN's "niladic" function syntax is quite lightweight, comparing favorably to a low-boilerplate language like Javascript.
+Even with these workarounds, BQN's "niladic" function syntax is lightweight, comparing favorably to a low-boilerplate language like Javascript.
fn = ()=>{m+=1;n*=2}; fn()
Fn ← {𝕀⋄ m+↩1,n×↩2}, Fn @
@@ -39,7 +39,7 @@ Control structures are called "statements" below to match common usage, but they
## If
-The if statement conditionally performs some action. It is similar to the Repeat (`⍟`) modifier with a right operand returning a boolean: `Fn⍟Cond 𝕩` gives `Fn 𝕩` if `Cond 𝕩` is `1`, and returns `𝕩` without calling `Fn` if `Cond 𝕩` is `0`. Here is how we might make it behave like a control structure.
+The if statement conditionally performs some action. It's similar to the [Repeat](repeat.md) (`⍟`) modifier with a right operand that returns a boolean: `Fn⍟Cond 𝕩` gives `Fn 𝕩` if `Cond 𝕩` is `1`, and returns `𝕩` without calling `Fn` if `Cond 𝕩` is `0`. Here's how we might make it behave like a control structure.
{𝕀⋄a+↩10}⍟(a<10) @
@@ -58,7 +58,7 @@ For a more conventional presentation, the condition and action can be placed in
The result of any of these if statements is the result of the action if it's performed, and otherwise it's whatever argument was passed to the statement, which is `@` or `10` here.
-BQN's syntax for a pure if statement isn't so good, but predicates handle [if-else](#if-else) statements nicely. So in most cases you'd forego the definitions above in favor of an if-else with nothing in the else branch:
+BQN's syntax for a pure if statement isn't so good, but [predicates](block.md#predicates) handle [if-else](#if-else) statements nicely. So in most cases you'd forego the definitions above in favor of an if-else with nothing in the else branch:
{ a<10 ? a+↩10 ; @ }
@@ -70,7 +70,7 @@ Another option is to use a [for-each](#for) statement with an argument of `↕n`
## If-Else
-In most cases, the easy way to write an if-else statement is with [predicates](block.md#predicates):
+In most cases, the easy way to write an if-else statement is with a [predicate](block.md#predicates):
{
threshold < 6 ?
@@ -78,7 +78,7 @@ In most cases, the easy way to write an if-else statement is with [predicates](b
b ↩ 1 Large threshold # If it wasn't
}
-We might also think of an if-else statement as a kind of [switch-case](#switch-case) statement, where the two cases are true (`1`) and false (`0`). As a result, we can implement it either with Choose (`β—Ά`) or with [case headers](block.md#case-headers) of `1` and `0`.
+We might also think of an if-else statement as a kind of [switch-case](#switch-case) statement, where the two cases are true (`1`) and false (`0`). As a result, we can implement it either with [Choose](choose.md) (`β—Ά`) or with [case headers](block.md#case-headers) of `1` and `0`.
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.
@@ -122,7 +122,7 @@ For a function-based approach, it's possible to nest `IfElse` expressions, but i
Test ⟨
( a<b)β€Ώ{𝕀⋄a+↩1}
{𝕀⋄a<c}β€Ώ{𝕀⋄c-↩1}
- {𝕀⋄a-↩2}
+ {𝕀⋄a-↩2}
⟩
## Switch-Case
@@ -139,7 +139,7 @@ The simplest way to write a switch-case statement is with [case headers](block.m
𝕩: nβˆΎβ†©π•©
}
-A simplified version of a switch-case statement is possible if the cases are natural numbers `0`, `1`, and so on. The Choose (`β—Ά`) modifier does just what we want. The `Select` statement below generalizes `IfElse`, except that it doesn't rearrange the cases relative to Choose while `IfElse` swaps them.
+A simplified version of a switch-case statement is possible if the cases are natural numbers `0`, `1`, and so on. The [Choose](choose.md) (`β—Ά`) modifier does just what we want. The `Select` statement below generalizes `IfElse`, except that it doesn't rearrange the cases relative to Choose while `IfElse` swaps them.
Select ← {(βŠ‘π•©)β—Ά(1↓𝕩)@}
@@ -153,7 +153,7 @@ A simplified version of a switch-case statement is possible if the cases are nat
To test against other possible values, the following statement takes interleaved lists of values and actions, and disentangles them. It searches through the values with `⊐`.
- Switch ← {cβ†βŠ‘π•© β‹„ mβ€Ώa←<Λ˜β‰βˆ˜β€Ώ2β₯Š1↓𝕩 β‹„ (m⊸⊐⌾<C)β—Άa@}
+ Switch ← {cβ†βŠ‘π•© β‹„ [m,a]β†β‰βˆ˜β€Ώ2β₯Š1↓𝕩 β‹„ (m⊸⊐⌾<C)β—Άa@}
Switch ⟨value
"increment" β‹„ {𝕀⋄ v+↩1}
@@ -166,14 +166,14 @@ Finally, the most general form of a switch statement is a [chained if-else](#cha
## Loop forever
-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 `π•¨π•Šπ•©` to the end:
+It's not a particularly common pattern, but this is a good simple case to warm up for the while loop. BQN [primitives](primitive.md) 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 `π•¨π•Šπ•©` to the end:
{
# Stuff to do forever
π•¨π•Šπ•©
} arg
-To convert this to a control structure format, we want to take an action `A`, and produce a function that runs `A`, then runs itself. Finally we want to call that function on some argument, say `@`. The argument is a single function, so to call Forever, we need to convert that function to a subject role.
+It's important to note that this won't actually run that many times before it fails with a stack overflow. We'll address that later. Anyway, to convert this to a control structure format, we want to take an action `A`, and produce a function that runs `A`, then runs itself. Finally we want to call that function on some argument, say `@`. The argument is a single function, so to call `Forever`, we need to convert that function to a subject role.
Forever ← {π•Ša:{π•ŠA𝕩}@}
@@ -185,7 +185,7 @@ A slicker method is to pass `𝕩` as an operand to a modifier. In a modifier, `
Forever ← {𝕩{π•Šπ”½π•©}@}
-The syntax here is awkward enough that it's actually better to use a while loop, with a constant condition of `1`!
+But the calling syntax is awkward enough that it's actually better to use a while loop, with a constant condition of `1`!
While 1β€Ώ{𝕀
# Stuff to do forever
@@ -213,11 +213,11 @@ The above version of `While` will fail in a fairly small number of iterations, b
While ← {𝕩{π”½βŸπ”Ύβˆ˜π”½_𝕣_π”Ύβˆ˜π”½βŸπ”Ύπ•©}𝕨@}Β΄
-The innovation is to use `{π”½βŸπ”Ύβˆ˜π”½_𝕣_π”Ύβˆ˜π”½βŸπ”Ύπ•©}` instead of the equivalent `{𝔽_𝕣_π”Ύβˆ˜π”½βŸπ”Ύπ•©}` or `{π•Šβˆ˜π”½βŸπ”Ύπ•©}` (these are the same, as `π•Š` in a modifier is defined as `𝔽_𝕣_𝔾`). Here `𝔽` performs one iteration and `𝔾` tests whether to continue. The simplest approach is to perform one iteration and recurse with the same two functions. The modified approach replaces `𝔽` with `π”½βŸπ”Ύβˆ˜π”½`, that is, it doubles it while making sure the condition is still checked each iteration. The doublings compound so that recursion level `n` performs `𝔽` up to `2⋆n` times while using on the order of `n` additional stack frames. Only a hundred or two stack frames are needed to give a practically unlimited number of iterations.
+The innovation is to use `{π”½βŸπ”Ύβˆ˜π”½_𝕣_π”Ύβˆ˜π”½βŸπ”Ύπ•©}` instead of the equivalent `{𝔽_𝕣_π”Ύβˆ˜π”½βŸπ”Ύπ•©}` or `{π•Šβˆ˜π”½βŸπ”Ύπ•©}` (these are the same, as `π•Š` in a modifier is defined to be `𝔽_𝕣_𝔾`). Here `𝔽` performs one iteration and `𝔾` tests whether to continue. The simplest approach is to perform one iteration, then recurse with the same two functions. The modified approach replaces `𝔽` with `π”½βŸπ”Ύβˆ˜π”½`, that is, it doubles it while making sure the condition is still checked each iteration. The doublings compound so that recursion level `n` performs `𝔽` up to `2⋆n` times while using on the order of `n` additional stack frames. Only a hundred or two stack frames are needed to give a practically unlimited number of iterations.
## For
-To begin with, are you sure you don't want a for-each loop instead? In BQN that's just a function with Each (`Β¨`), and it covers most common uses of a for loop.
+To begin with, are you sure you don't want a for-each loop instead? In BQN that's just a function with [Each](map.md) (`Β¨`), and it covers most common uses of a for loop.
FnΒ¨ v # for (𝕩 in v)
FnΒ¨ ↕n # for (𝕩=0; 𝕩<n; 𝕩++)
@@ -237,9 +237,9 @@ Very well… a for loop is just a while loop with some extra pre- and post-actio
}
}
-The initialization can be a simple expression as shown; in fact it's a little silly to make initialization one of the arguments to `For` at all.Unlike in C, it's impossible to declare a variable that's local to the whole `For` loop but not its surroundings. Hopefully this is obvious from the structure of the code! Only curly braces can create a new scope, so to localize some variables in the `For` loop, just surround it in an extra set of curly braces.
+The initialization can be a simple expression as shown; in fact it's a little silly to make initialization one of the arguments to `For` at all. Unlike in C, it's impossible to declare a variable that's local to the whole `For` loop but not its surroundings. Hopefully this is obvious from the structure of the code! Only curly braces can create a new scope, so to localize some variables in the `For` loop, surround it in an extra set of curly braces.
-The `While` loop alone allows syntax similar to the `For` loop. Perform any initialization outside of the loop, and compose the post-action with the main body using the reverse composition `{π”Ύβˆ˜π”½}`. Because the composition binds less tightly than stranding, the bracketed [list notation](arrayrepr.md#brackets) has to be used here.
+The `While` loop alone allows syntax similar to the `For` loop. Perform any initialization outside of the loop, and compose the post-action with the main body using the reverse composition `{π”Ύβˆ˜π”½}`. Because that composition would bind less tightly than stranding, the bracketed [list notation](arrayrepr.md#brackets) has to be used here.
c←27 β‹„ n←0
While ⟨{𝕀⋄1<c}, {𝕀⋄n+↩1}{π”Ύβˆ˜π”½}{𝕀