diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2022-06-26 21:00:25 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2022-06-26 21:00:25 -0400 |
| commit | b6185d5029e2adcc721c0cc2097f591d9a09f135 (patch) | |
| tree | bf777353ed2a9b28d8b1577c5f36b68605240375 /doc/block.md | |
| parent | c618ade174cc2b4e428457751ad8dd01130c2239 (diff) | |
I am in editing stepped in so far that, should I wade no more, returning were as tedious as go o'er.
Diffstat (limited to 'doc/block.md')
| -rw-r--r-- | doc/block.md | 56 |
1 files changed, 36 insertions, 20 deletions
diff --git a/doc/block.md b/doc/block.md index 8cdaed9c..823007f1 100644 --- a/doc/block.md +++ b/doc/block.md @@ -2,14 +2,16 @@ # 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. An important aspect of organization is [namespaces](namespace.md), which are created with blocks but not discussed on this page. +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. One organizing tool not discussed here is [namespaces](namespace.md), which are created with blocks but have their own page. Programming without blocks (only recommended at the small scale) is called [tacit](tacit.md) programming. 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 Γ{π©π½π©} 4 -Because they use [lexical scoping](lexical.md), 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 block [header](#block-headers) is written before a `:` and describes the block type, and what inputs it accepts. A block can be split into [multiple bodies](#multiple-bodies) using `;`s, so that each handles different cases. A [predicate](#predicates), written with `?`, can test an arbitrary condition to refine these cases. + +Because they use [lexical scoping](lexical.md), blocks also 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β"inner" β aβΏb } @@ -21,7 +23,7 @@ In the simplest case a block is just a list of statements, which are executed to updown β { upββ5 β downββ½up β upβΎdown } updown -An immediate block is only ever evaluated once, and can't be used for control flow in a program. Including special names in a headerless block lets us define functions and modifiers, which have a broader range of uses. All special names are listed below: +An immediate block is only ever evaluated once, and can't be used for control flow in a program. Special names can be used to define [functions and modifiers](ops.md), which have a broader range of uses. All special names are listed below: | Lowercase | Uppercase | Meaning |-----------|-----------|--------- @@ -32,27 +34,30 @@ An immediate block is only ever evaluated once, and can't be used for control fl | `π` | `πΎ` | Right [operand](#operands) | `π£` | none | Modifier [self-reference](#self-reference) -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. This allows a modifier to be applied to a special name with no spacing, as in `π_m`, where it couldn't be with ordinary names. +Most special names have a lowercase form for a subject [role](expression.md#syntactic-role) and uppercase for a function role. But `π£` is sort of a "more special" character, as we'll discuss below. The special names other than `π£` are single characters that don't attach to other letters, allowing `π½π©` or `π_m` to work without spaces; `π£` is always modifier-valued, so it ought to attach to underscores. ### 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. Having either of these names turns 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. Their values can be changed like ordinary variables. {'c'=π©} "abcd" + { π©+β©2 β 0βπ© } 3 - 4 { β¨π©β-π¨β© } 5 + + 4 { β¨π©,-π¨β© } 5 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](expression.md#nothing), or `Β·`. This is the only time a variable can ever be Nothing, as an assignment such as `vβΒ·` is not allowed. 3 { (2Γπ¨)-π© } 1 { (2Γπ¨)-π© } 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. +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](train.md). "abc" { (π¨ββ½) π© } "def" { (π¨ββ½) π© } "def" -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. +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](../problems.md#nothing--interacts-strangely-with-before-and-after) is that `βΈ` and `β` don't work the way you might expect. + { π¨ ββΈ- π© } 5 @@ -79,22 +84,22 @@ The distinction between an immediate and deferred modifier only matters inside t ### Self-reference -If a block is assigned a name after it is created, this name can be used for recursion: +If a block is assigned a name after it's created, this name can be used for recursion: Fact β { π© Γ (0βΈ<)βΆ1βΏFact π©-1 } Fact 7 (ΓΒ΄1+β) 7 # There's often a simpler solution than recursion -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. +This is somewhat unsatisfying because the name is external to the function being defined, but the definition shouldn'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 -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. +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's usually done with a second `factorial_helper` function in elementary Scheme (BQN doesn't optimize tail recursion though; it's just shown here as an example). Fact_mod β 1 { (0βΈ<)βΆβ¨π, (πΓπ©)_π£β© π©-1 } Fact_mod 7 -Because `π£` only ever refers to a 1-modifier or 2-modifer, it can never make sense to refer to it as a function, and the uppercase letter `β` is not recognized by BQN. In order to allow `π£` to be spelled as a 1-modifier `_π£` or 2-modifier `_π£_`, it is treated as an ordinary identifier character, so it must be separated from letters or numbers by spaces. +Because `π£` only ever refers to a 1-modifier or 2-modifer, it can never make sense to refer to it as a function, and the uppercase letter `β` is not recognized by BQN. To allow `π£` to be spelled as a 1-modifier `_π£` or 2-modifier `_π£_`, it's tokenized as an ordinary identifier character, so it has to be separated from adjacent letters or numbers with a space. ## Block headers @@ -122,7 +127,7 @@ Its syntax mirrors an application of the block. As suggested by the positioning, { F _op_ Β·βΏval: β¦ -In all cases special names still work just like in a headerless function. In this respect the effect of the header is the same as a series of assignments at the beginning of a function, such as the following translation of the second header above: +In all cases special names still work just like in a headerless function. In this respect, the effect of the header is the same as a series of assignments at the beginning of a function, such as the following translation of the second header above: { # Fn _apply β¨a,bβ©: Fn β π½ @@ -130,15 +135,17 @@ In all cases special names still work just like in a headerless function. In thi β¨a,bβ© β π© β¦ -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. +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, so 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 and operands allow [destructuring](expression.md#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. +Arguments and operands allow [destructuring](expression.md#destructuring) like assignment does. While assignment only tolerates lists of variables, header destructuring also allows constants. For the header to match, the argument must share the given structure, including the constants where they appear. Destruct β { π aβΏ1βΏβ¨b,Β·,2β©: aβb } Destruct 5βΏ1βΏβ¨7,Ο,2β© +It's also worth noting here that `[]` is a valid destructuring target, matching any length-0 array, even though it can't be used as a value since it's ambiguous. This syntax is also allowed in regular destructuring, but it's not very useful in that case. + ### Special names in headers Any element of a function or modifier header can be left nameless by using the corresponding special name in that position, instead of an identifier. For example, the header `π¨ π½_π£_πΎ π©:` incorporates as much vagueness as possible. It indicates a deferred 2-modifier, but provides no other information. @@ -154,21 +161,26 @@ A header can also be a plain name with no inputs, called a *label*. A label spec { _π£: # 1-Modifier { _π£_: # 2-Modifier -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, the name can't be used in code: it doesn't make sense to refer to a value while it is still being computed! +For immediate blocks, this is the only type of header possible, and it must use an identifier as there's no applicable special name. However, the name can't be used in code: it doesn't make sense to refer to a value while it's still being computed! ## Multiple bodies -Blocks 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 is that functions and deferred modifiers can have two headerless bodies (that is, no headers or predicatesβsee below): the first applies when there's one argument and the second when there are two. +Blocks can include more than one body, separated by semicolons `;`. The body used for a particular evaluation is chosen based on the inputs to the block. One special case is that functions and deferred modifiers can have two headerless bodies (that is, no headers or [predicates](#predicates)): the first applies when there's one argument and the second when there are two. Ambiv β { β¨1,π©β© ; β¨2,π¨,π©β© } + Ambiv 'a' + 'a' Ambiv 'b' Bodies with headers come before any that don't have them. When a block is called, its headers are checked in order for compatibility with the arguments, and the first body with a compatible header is used. CaseAdd β { 2π3:0βΏ5 ; 2ππ©:β¨1,2+π©β© ; ππ©:2βΏπ© } + 2 CaseAdd 3 + 2 CaseAdd 4 + CaseAdd 4 If no header is compatible, the call results in an error. @@ -190,25 +202,29 @@ These case-style headers function exactly the same as if they were preceded by ` ### Predicates -Destructuring with a header is quite limited, only allowing matching structure and data with exact equality. A predicate, written with `?`, allows you to test an arbitrary property before evaluating the rest of the body, and also serves as a limited kind of control flow. It can be thought of as an extension to a header, so that for example the following function requires the argument to have two elements and for the first to be less than the second before using the first body. Otherwise it moves to the next body, which is unconditional. +Destructuring with a header is limited, as it can only match a particular structure or value exactlyβnot, for example, a range of lengths. A predicate, written with `?`, allows you to test an arbitrary property before evaluating the rest of the body, and also serves as a limited kind of control flow. It can be thought of as an extension to a header. So the following function requires the argument to have two elements and for the first to be less than the second before using the first body. Otherwise it moves to the next body, which is unconditional. CheckPair β { πβ¨a,bβ©: a<b? "ok" ; "not ok" } CheckPair β¨3,8β© # Fails destructuring + CheckPair β¨1,4,5β© # Not a pair + CheckPair β¨3,Β―1β© # Not ascending -The body where the predicate appears doesn't need to start with a header, and there can be other statements before it. In fact, `?` functions just like a separator (like `β` or `,`) with a side effect. +The body where the predicate appears doesn't need to start with a header, and there can be other statements before it. Really, `?` works just like a separator (like `β` or `,`) with a side effect. { rββ½π© β 't'=βr ? r ; π© }Β¨ "test"βΏ"this" -So `r` is the reversed argument, and if its first character (the last one in `π©`) is `'t'` then it returns `r`, and otherwise we abandon that line of reasoning and return `π©`. This sounds a lot like an if statement. And `{ a<b ? a ; b }`, which computes `aβb` the hard way, shows how the syntax can be similar to a ternary operator. This is an immediate block with multiple bodies, something that makes sense with predicates but not headers. But `?;` offers more possibilities. It can support any number of options, with multiple tests for each oneβthe structure below is "if \_ and \_ then \_; else if \_ then \_; else \_". +So `r` is the reversed argument, and if its first character (the last one in `π©`) is `'t'` then it returns `r`, and otherwise we abandon that line of reasoning and return `π©`. + +This sounds a lot like an if statement. And `{ a<b ? a ; b }`, which computes `aβb` the hard way, shows how the syntax can be similar to a ternary operator. This is an immediate block with multiple bodies, something that makes sense with predicates but not headers. But `?;` offers more possibilities. It can support any number of options, with multiple tests for each oneβthe structure below is "if \_ and \_ then \_; else if \_ then \_; else \_". Thing β { π©β₯3? π©β€8? 2|π© ; π©=0? @ ; β } (β’ β ThingΒ¨) β10 # Table of arguments and results -This structure is still constrained by the rules of block bodies: each instance of `;` is a separate scope, so that variables defined before a `?` don't survive past the `;`. +This structure is still controlled by the rules of block bodies: each instance of `;` is a separate scope, so that variables defined before a `?` don't survive past the `;`. { 0=nββ π© ? β ; n } "abc" |
