From 1d6a9cf1441bd6d478977715d82031e77c20ce5c Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Mon, 26 Oct 2020 15:46:38 -0400 Subject: Don't include &run in documentation REPL links: it's no longer used --- docs/doc/block.html | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'docs/doc/block.html') 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 @@

Blocks

In BQN, a block is any piece of code surrounded with curly braces {}. 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.

Blocks are most commonly used to define functions by including one of the special names for arguments, ๐•จ or ๐•ฉ. With the operands ๐”ฝ or ๐”พ, they can also define 1-modifiers or 2-modifiers.

-โ†—๏ธ
    {๐•ฉ+1} 3
+โ†—๏ธ
    {๐•ฉ+1} 3
 4
     ร—{๐•ฉ๐”ฝ๐•ฉ} 4
 16
 

Because they use lexical scoping, 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.

-โ†—๏ธ
    aโ†bโ†"outer"
+โ†—๏ธ
    aโ†bโ†"outer"
     { aโ†"inner" โ‹„ aโ€ฟb }
 โŸจ "inner" "outer" โŸฉ
 

Headerless blocks

In the simplest case a block is just a list of statements, which are executed to evaluate the block. A block with no special names like ๐•จ or ๐•ฉ is called an immediate block, 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.

-โ†—๏ธ
    updown โ† { upโ†โ†•5 โ‹„ downโ†โŒฝup โ‹„ upโˆพdown }
+โ†—๏ธ
    updown โ† { upโ†โ†•5 โ‹„ downโ†โŒฝup โ‹„ upโˆพdown }
     updown
 โŸจ 0 1 2 3 4 4 3 2 1 0 โŸฉ
 
@@ -68,7 +68,7 @@

Of these, ๐•ฃ is sort of a "more special" character, as we'll discuss below. Except for ๐•ฃ, 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 ๐•—_m, something that can't be done with ordinary names.

Arguments

The names ๐•จ and ๐•ฉ, 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.

-โ†—๏ธ
    {'c'=๐•ฉ} "abcd"
+โ†—๏ธ
    {'c'=๐•ฉ} "abcd"
 โŸจ 0 0 1 0 โŸฉ
     { ๐•ฉ+โ†ฉ2 โ‹„ 0โˆพ๐•ฉ } 3
 โŸจ 0 5 โŸฉ
@@ -76,13 +76,13 @@
 โŸจ 5 ยฏ4 โŸฉ
 

A function with ๐•จ in its definition doesn't have to be called with two arguments. If it has only one, then ๐•จ is given the special value Nothing ยท. This is the only time a variable can ever be Nothing, as an assignment such as vโ†ยท is not allowed.

-โ†—๏ธ
    3 { (2ร—๐•จ)-๐•ฉ } 1
+โ†—๏ธ
    3 { (2ร—๐•จ)-๐•ฉ } 1
 5
       { (2ร—๐•จ)-๐•ฉ } 1
 ยฏ1
 

In the second function, ๐•จ behaves just like ยท, so that the function 2ร—๐•จ is not evaluated and - doesn't have a left argument. It has a similar effect when used as the left argument to a function in a train.

-โ†—๏ธ
    "abc" { (๐•จโ‰โŒฝ) ๐•ฉ } "def"
+โ†—๏ธ
    "abc" { (๐•จโ‰โŒฝ) ๐•ฉ } "def"
 โ”Œโ”€     
 โ•ต"abc  
   fed" 
@@ -93,24 +93,24 @@
       โ”˜
 

However, ยท can only be used as an argument, and not a list element or operand. Don't use ๐•จ in these ways in a function that could be called monadically. Another potential issue is that โŠธ and โŸœ don't work the way you might expect.

-โ†—๏ธ
    { ๐•จ โ‹†โŠธ- ๐•ฉ } 5
+โ†—๏ธ
    { ๐•จ โ‹†โŠธ- ๐•ฉ } 5
 143.413159102577
 

Called dyadically, this function will expand to (โ‹†๐•จ)-๐•ฉ, so we might expect the monadic result to be -๐•ฉ. This sort of expansion isn't right with ยท on the left. โ‹†โŠธ- taken as a whole is a function, so ยท โ‹†โŠธ- ๐•ฉ is just โ‹†โŠธ- ๐•ฉ, or (โ‹†๐•ฉ)-๐•ฉ, giving the large result seen above.

Operands

The special names ๐”ฝ and ๐”พ, and their lowercase forms, represent operands. Since operands are more often functions, they're typically shown with the uppercase spelling. If ๐”ฝ is present in a block then it defines a 1-modifier or 2-modifier depending on whether ๐”พ is present; if ๐”พ is there it's always a 2-modifier.

-โ†—๏ธ
    4 {ร—หœ๐•—}
+โ†—๏ธ
    4 {ร—หœ๐•—}
 16
     2 {๐•—+๐•˜} 3
 5
 

As shown above, modifiers without ๐•จ, ๐•ฉ, or ๐•ค behave essentially like functions with a higher precedence. These immediate modifiers 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.

-โ†—๏ธ
    _dot_ โ† {๐”ฝยดโˆ˜๐”พ}
+โ†—๏ธ
    _dot_ โ† {๐”ฝยดโˆ˜๐”พ}
     1โ€ฟ2โ€ฟ3 +_dot_ร— 1โ€ฟ0โ€ฟ1
 4
 

However, if one of these names is included, then a deferred modifier 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.

-โ†—๏ธ
    +{๐•ฉ๐”ฝ๐•ฉ} 6
+โ†—๏ธ
    +{๐•ฉ๐”ฝ๐•ฉ} 6
 12
     2 โฅŠ{โŸจ๐”ฝ๐•จ,๐”พ๐•ฉโŸฉ}- 5
 โŸจ โŸจ 2 โŸฉ ยฏ5 โŸฉ
@@ -118,18 +118,18 @@
 

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.

Self-reference

If a block is assigned a name after it is created, this name can be used for recursion:

-โ†—๏ธ
    Fact โ† { ๐•ฉ ร— (0โŠธ<)โ—ถ1โ€ฟFact ๐•ฉ-1 }
+โ†—๏ธ
    Fact โ† { ๐•ฉ ร— (0โŠธ<)โ—ถ1โ€ฟFact ๐•ฉ-1 }
     Fact 7
 5040
     (ร—ยด1+โ†•) 7  # There's often a simpler solution than recursion
 5040
 

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 ๐•Š can be used to refer to the function it appears in. This allows anonymous recursive functions to be defined.

-โ†—๏ธ
    { ๐•ฉ ร— (0โŠธ<)โ—ถ1โ€ฟ๐•Š ๐•ฉ-1 } 7
+โ†—๏ธ
    { ๐•ฉ ร— (0โŠธ<)โ—ถ1โ€ฟ๐•Š ๐•ฉ-1 } 7
 5040
 

For modifiers, ๐•ฃ refers to the containing modifier. ๐•Š makes the modifier a deferred modifier like ๐•จ and ๐•ฉ 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 factorial_helper function in elementary Scheme.

-โ†—๏ธ
    Fact_mod โ† 1 { (0โŠธ<)โ—ถโŸจ1, (๐•จร—๐•ฉ)_๐•ฃโŸฉ ๐•ฉ-1 }
+โ†—๏ธ
    Fact_mod โ† 1 { (0โŠธ<)โ—ถโŸจ1, (๐•จร—๐•ฉ)_๐•ฃโŸฉ ๐•ฉ-1 }
     Fact_mod 7
 1
 
@@ -162,7 +162,7 @@

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 ๐”พ or ๐•จ. Calling it with a left argument, or a right argument that isn't a two-element list, will result in an error.

Destructuring

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.

-โ†—๏ธ
    Destruct โ† { ๐•Š aโ€ฟ1โ€ฟโŸจb,2โŸฉ: aโ‰b }
+โ†—๏ธ
    Destruct โ† { ๐•Š aโ€ฟ1โ€ฟโŸจb,2โŸฉ: aโ‰b }
     Destruct       5โ€ฟ1โ€ฟโŸจ7,2โŸฉ
 โŸจ 5 7 โŸฉ
 
@@ -179,14 +179,14 @@

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 returns: it doesn't make sense to refer to a value while it is still being computed!

Multiple bodies

Blocks that define functions and deferred modifiers can include more than one body, separated by semicolons ;. 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.

-โ†—๏ธ
    Ambiv โ† { โŸจ1,๐•ฉโŸฉ ; โŸจ2,๐•จ,๐•ฉโŸฉ }
+โ†—๏ธ
    Ambiv โ† { โŸจ1,๐•ฉโŸฉ ; โŸจ2,๐•จ,๐•ฉโŸฉ }
     Ambiv 'a'
 โŸจ 1 'a' โŸฉ
     'a' Ambiv 'b'
 โŸจ 2 'a' 'b' โŸฉ
 

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.

-โ†—๏ธ
    CaseAdd โ† { 2๐•Š3:0โ€ฟ5 ; 2๐•Š๐•ฉ:โŸจ1,2+๐•ฉโŸฉ ; ๐•Š๐•ฉ:2โ€ฟ๐•ฉ }
+โ†—๏ธ
    CaseAdd โ† { 2๐•Š3:0โ€ฟ5 ; 2๐•Š๐•ฉ:โŸจ1,2+๐•ฉโŸฉ ; ๐•Š๐•ฉ:2โ€ฟ๐•ฉ }
     2 CaseAdd 3
 โŸจ 0 5 โŸฉ
     2 CaseAdd 4
-- 
cgit v1.2.3