From 0c716e4c6b7c2c44bbfd02b6503cae66af7b7480 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 28 Jan 2022 16:34:41 -0500 Subject: Separate syntax highlighting category for header/body characters ;:? --- docs/doc/control.html | 56 +++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) (limited to 'docs/doc/control.html') diff --git a/docs/doc/control.html b/docs/doc/control.html index 5abfa166..b4426630 100644 --- a/docs/doc/control.html +++ b/docs/doc/control.html @@ -10,26 +10,26 @@

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.

If      โ† {๐•โŸ๐•Ž@}ยด                 # Also Repeat
-IfElse  โ† {cโ€ฟTโ€ฟF: cโ—ถFโ€ฟT@}
+IfElse  โ† {cโ€ฟTโ€ฟF: cโ—ถFโ€ฟT@}
 While   โ† {๐•ฉ{๐”ฝโŸ๐”พโˆ˜๐”ฝ_๐•ฃ_๐”พโˆ˜๐”ฝโŸ๐”พ๐•ฉ}๐•จ@}ยด  # While 1โ€ฟ{... to run forever
 DoWhile โ† {๐•@ โ‹„ While ๐•จโ€ฟ๐•ฉ}ยด
-For     โ† {Iโ€ฟCโ€ฟPโ€ฟA: I@ โ‹„ WhileโŸจC,Pโˆ˜AโŸฉ}
+For     โ† {Iโ€ฟCโ€ฟPโ€ฟA: I@ โ‹„ WhileโŸจC,Pโˆ˜AโŸฉ}
 
 # Switch/case statements have many variations; these are a few
 Match   โ† {๐•๐•จ}ยด
 Select  โ† {(โŠ‘๐•ฉ)โ—ถ(1โ†“๐•ฉ)@}
 Switch  โ† {cโ†โŠ‘๐•ฉ โ‹„ mโ€ฟaโ†<ห˜โ‰โˆ˜โ€ฟ2โฅŠ1โ†“๐•ฉ โ‹„ (โŠ‘aโАC)โ—ถm@}
-Test    โ† {fnโ†{Cโ€ฟA๐•Še:Cโ—ถAโ€ฟE}ยด๐•ฉโ‹„Fn@}
+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 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:

Even with these workarounds, BQN's "niladic" function syntax is quite lightweight, comparing favorably to a low-boilerplate language like Javascript.

-
fn = ()=>{m+=1;n*=2}; fn()
+
fn = ()=>{m+=1;n*=2}; fn()
 Fn โ† {๐•คโ‹„  m+โ†ฉ1,nร—โ†ฉ2}, Fn @
 

Control structures are called "statements" below to match common usage, but they are actually expressions, and return a value that might be used later.

@@ -49,7 +49,7 @@

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 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 ; @ }
+
{ a<10 ? a+โ†ฉ10 ; @ }
 

Repeat

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.

@@ -57,14 +57,14 @@

If-Else

In most cases, the easy way to write an if-else statement is with predicates:

{
-  threshold < 6 ?
-  a โ†ฉ Small threshold ;  # If predicate was true
+  threshold < 6 ?
+  a โ†ฉ Small threshold ;  # If predicate was true
   b โ†ฉ 1 Large threshold  # If it wasn't
 }
 

We might also think of an if-else statement as a kind of 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 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.

-
IfElse โ† {condโ€ฟTrueโ€ฟFalse: condโ—ถFalseโ€ฟTrue @}
+
IfElse โ† {condโ€ฟTrueโ€ฟFalse: condโ—ถFalseโ€ฟTrue @}
 
 IfElse โŸจ๐•ฉ<midโŠ‘๐•จ
   {๐•คโ‹„ hiโ†ฉmid}
@@ -79,22 +79,22 @@
 

Case headers have similar syntax, but the two cases are labelled explicitly. In this form, the two actions are combined in a single function, which could be assigned to call it on various conditions.

{๐•๐•จ}ยด (๐•ฉ<midโŠ‘๐•จ)โ€ฟ{
-  1: hiโ†ฉmid
-;
-  0: loโ†ฉmid
+  1: hiโ†ฉmid
+;
+  0: loโ†ฉmid
 }
 

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.

Chained If-Else

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 if statement followed by a sequence of else if "statements", or might just provide a unified elif keyword that works similarly. BQN's predicates work really well for this structure:

{
-  a<b ? a+โ†ฉ1 ;
-  a<c ? c-โ†ฉ1 ;
+  a<b ? a+โ†ฉ1 ;
+  a<c ? c-โ†ฉ1 ;
         a-โ†ฉ2
 }
 

For a function-based approach, it's possible to nest IfElse 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 โŸจTest,ActionโŸฉ 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.

-
Test โ† {fnโ†{Condโ€ฟAct ๐•Š else: Condโ—ถElseโ€ฟAct}ยด๐•ฉ โ‹„ Fn@}
+
Test โ† {fnโ†{Condโ€ฟAct ๐•Š else: Condโ—ถElseโ€ฟAct}ยด๐•ฉ โ‹„ Fn@}
 
 Test โŸจ
   (  a<b)โ€ฟ{๐•คโ‹„a+โ†ฉ1}
@@ -107,11 +107,11 @@
 
Match โ† {๐•๐•จ}ยด
 
 Match valueโ€ฟ{
-  0โ€ฟb: n-โ†ฉb
-;
-  aโ€ฟb: n+โ†ฉa-b
-;
-  ๐•ฉ: nโˆพโ†ฉ๐•ฉ
+  0โ€ฟb: n-โ†ฉb
+;
+  aโ€ฟb: n+โ†ฉa-b
+;
+  ๐•ฉ: 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.

@@ -144,7 +144,7 @@ } 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.

-
Forever โ† {๐•Ša:{๐•ŠA๐•ฉ}@}
+
Forever โ† {๐•Ša:{๐•ŠA๐•ฉ}@}
 
 Forever 1โŠ‘@โ€ฟ{๐•ค
   # Stuff to do forever
@@ -184,13 +184,13 @@
 Fnยจ โŒฝโ†•n    # for (๐•ฉ=n; --๐•ฉ; )
 

Very wellโ€ฆ a for loop is just a while loop with some extra pre- and post-actions.

-
For โ† {Preโ€ฟCondโ€ฟPostโ€ฟAct: Pre@ โ‹„ {๐•Šโˆ˜Postโˆ˜ActโŸCond ๐•ฉ}@}
+
For โ† {Preโ€ฟCondโ€ฟPostโ€ฟAct: Pre@ โ‹„ {๐•Šโˆ˜Postโˆ˜ActโŸCond ๐•ฉ}@}
 
 For (cโ†27โŠฃnโ†0)โ€ฟ{๐•คโ‹„1<c}โ€ฟ{๐•คโ‹„n+โ†ฉ1}โ€ฟ{๐•ค
   {๐•๐•จ}ยด (2|c)โ€ฟ{
-    0: cรทโ†ฉ2
-  ;
-    1: cโ†ฉ1+3ร—c
+    0: cรทโ†ฉ2
+  ;
+    1: cโ†ฉ1+3ร—c
   }
 }
 
@@ -199,9 +199,9 @@
cโ†27 โ‹„ nโ†0
 While โŸจ{๐•คโ‹„1<c}, {๐•คโ‹„n+โ†ฉ1}{๐”พโˆ˜๐”ฝ}{๐•ค
   Match (2|c)โ€ฟ{
-    0: cรทโ†ฉ2
-  ;
-    1: cโ†ฉ1+3ร—c
+    0: cรทโ†ฉ2
+  ;
+    1: cโ†ฉ1+3ร—c
   }
 }โŸฉ
 
-- cgit v1.2.3