diff options
| -rw-r--r-- | doc/shift.md | 16 | ||||
| -rw-r--r-- | doc/swap.md | 8 | ||||
| -rw-r--r-- | doc/syntax.md | 34 | ||||
| -rw-r--r-- | doc/types.md | 6 | ||||
| -rw-r--r-- | docs/doc/shift.html | 24 | ||||
| -rw-r--r-- | docs/doc/swap.html | 8 | ||||
| -rw-r--r-- | docs/doc/syntax.html | 33 | ||||
| -rw-r--r-- | docs/doc/types.html | 6 |
8 files changed, 72 insertions, 63 deletions
diff --git a/doc/shift.md b/doc/shift.md index bd51edce..bf28931c 100644 --- a/doc/shift.md +++ b/doc/shift.md @@ -2,19 +2,21 @@ # Shift functions -The shift functions `«` and `»` add major cells to one side an array, displacing cells on the opposite side and moving those in between. Shifts resemble but are more general than the bit-based shift operations used in low-level languages. They replace the APL pattern of a 2-wise reduction after appending or prepending an element (APL's `2≠/0,v` translates to `»⊸≠v`), one of the more common uses of 2-wise reduction. +The shift functions `«` and `»` add [major cells](array.md#cells) to one side an array, displacing cells on the opposite side and moving those in between. Shifts resemble but are more general than the bit-based shift operations used in low-level languages. They replace the APL pattern of a 2-wise reduction after appending or prepending an element (APL's `2≠/0,v` translates to `»⊸≠v`); a nice version of this common pattern is one reason BQN is free to replace windowed reduction with the sometimes less convenient [Windows](windows.md). The result of a shift function always has the same shape as `𝕩`. The function adds major cells to the beginning (`»`) or end (`«`) of `𝕩`, moving the cells already in `𝕩` to accomodate them. Some cells on the opposite side from those added will "fall off" and not be included in the result. 0‿0 » 3‿2‿1 # Shift Before + "end" « "add to the " # Shift After The cells to add come from `𝕨` if it's present, as shown above. Otherwise, a single cell of [fill elements](fill.md) for `𝕩` is used. This kind of shift, which moves cells in `𝕩` over by just one, is called a "nudge". » "abcd" # Nudge + « 1‿2‿3 # Nudge Back -If `𝕨` is longer than `𝕩`, some cells from `𝕨` will be discarded, as well as all of `𝕩`. In this case `𝕨»𝕩` is `(≠𝕩)↑𝕨` and `𝕨«𝕩` is `(-≠𝕩)↑𝕨`. For similar reasons, nudging an array of length 0 returns it unchanged. +If `𝕨` is longer than `𝕩`, some cells from `𝕨` will be discarded, plus all of `𝕩`. In this case `𝕨»𝕩` is `(≠𝕩)↑𝕨` and `𝕨«𝕩` is `(-≠𝕩)↑𝕨`. For similar reasons, nudging an array of length 0 returns it unchanged. ## Sequence processing with shifts @@ -24,7 +26,7 @@ When working with a sequence of data such as text, daily measurements, or audio s ≍ »s -⟜» s - +` -⟜» s + +` -⟜» s # Same as s In this way `»` refers to a sequence containing the previous element at each position. By default the array's fill is used for the element before the first, and a right argument can be given to provide a different one. @@ -49,7 +51,7 @@ A feature these examples all share is that they maintain the length of `s`. This ## Arithmetic and logical shifts -The glyphs `«` and `»`, suggesting movement, were chosen for the same reasons as the digraphs `<<` and `>>`, and can be used to implement the same operations on boolean lists. +The glyphs `«` and `»`, suggesting movement, were chosen for the same reasons as the digraphs `<<` and `>>` in C-like languages, and can be used to implement the same bit-shift operations on boolean lists. ⊢ i ← "10011011"-'0' @@ -65,7 +67,7 @@ With a number in big-endian format, a right shift might be logical, shifting in ## Other examples -In [Take](take.md) (`↑`), there's no way to specify the fill element when the result is longer than the argument. To take along the first axis with a specified, constant fill value, you can use Shift Before instead, where the right argument is an array of fills with the desired final shape. +In [Take](take.md) (`↑`), there's no way to specify the fill element when the result is longer than the argument. To take along the first axis with a specified, constant fill value, you can use Shift Before instead, where the right argument is an array of fills with the desired final shape (a more general approach is [Under](under.md)). "abc" » 5⥊'F' @@ -77,13 +79,13 @@ When using [Scan](scan.md) (`` ` ``), the result at a particular index is obtain 2 +`∘» 1‿0‿1‿0 # Final value not created -The *strides* of an array are the distances between one element and the next along any given axis. It's the product of all axis lengths below that axis, since these are all the axes that have to be "skipped" to jump along the axis. The strides of an array `𝕩` are `` (×`1»⊢)⌾⌽∘≢𝕩 ``. +The *strides* of an array are the distances between one element and the next along any given axis. It's the product of all axis lengths below that axis, since these are all the axes that have to be "skipped" to jump along the axis. The strides of an array `𝕩` are `` (×`1»⊢)⌾⌽ ≢𝕩 ``. (×`1»⊢)⌾⌽ 5‿2‿4‿3 ## Higher rank -Shifting always works on the first axis of `𝕩` (which must have rank 1 or more), and shifts in major cells. A left argument can have rank equal to `𝕩`, or one less than it, in which case it becomes a single cell of the result. With no left argument, a cell of fills `1↑0↑𝕩` is nudged in. +Shifting always works on the [first axis](leading.md) of `𝕩` (which must have rank 1 or more), and shifts in major cells. A left argument can have rank equal to `𝕩`, or one less than it, in which case it becomes a single cell of the result. With no left argument, a cell of fills `1↑0↑𝕩` is nudged in. ⊢ a ← ⥊⟜(↕×´) 4‿3 diff --git a/doc/swap.md b/doc/swap.md index 0d9245e3..a6df2a29 100644 --- a/doc/swap.md +++ b/doc/swap.md @@ -13,18 +13,18 @@ Have the arguments to a function, but not in the right places? Self/Swap (`˜`) | Self | ` F˜𝕩` | `𝕩F𝕩` | Swap | `𝕨F˜𝕩` | `𝕩F𝕨` -Since `𝕩` is always the left argument, these two definitions can be unified as `{𝕩𝔽𝕨⊣𝕩}`, noting that [Left](identity.md) becomes a plain identity function when the left argument `𝕨` isn't given. +Since `𝕩` always becomes the left argument, these two definitions can be unified as `{𝕩𝔽𝕨⊣𝕩}`, noting that [Left](identity.md) returns `𝕨` if it's given and `𝕩` if not. -Swap is arguably less transformative. Some common examples are `-˜` and `÷˜`, since these two functions run the [wrong way](../commentary/problems.md#subtraction-division-and-span-are-backwards) for BQN's evaluation order. This is very often useful in [tacit](tacit.md) programming, and less useful for explicit code. While it sometimes allows for shorter code by making a pair of parentheses unnecessary (say, `(a×b)-c` is `c-˜a×b`), I personally don't think this is always a good idea. My opinion is that it should be used when it makes the semantics a better fit for BQN, but putting the primary argument on the right and a secondary or control argument on the left. +Swap is arguably less transformative. Some common examples are `-˜` and `÷˜`, since these two functions run the [wrong way](../commentary/problems.md#subtraction-division-and-span-are-backwards) for BQN's evaluation order. This is very often useful in [tacit](tacit.md) programming, and less needed for explicit code. While it sometimes allows for shorter code by making a pair of parentheses unnecessary (say, `(a×b)-c` is `c-˜a×b`), I personally don't think this is always a good idea. My opinion is that it should be used when it makes the semantics a better fit for BQN, but putting the primary argument on the right and a secondary or control argument on the left. 'a' ⋈˜ 'b' " +" ⊏˜ 0‿1‿1‿0‿0≍1‿0‿1‿0‿1 -Self re-uses one argument twice. In this way it's a little like [Over](compose.md), which re-uses one *function* twice. A common combination is with Table, `⌜˜`, so that the operand function is called on each combination of elements from the argument to form a square result. For example, `=⌜˜` applied to `↕n` gives the identity matrix of size `n`. +Moving on, Self re-uses one argument twice. In this way it's a little like [Over](compose.md), which re-uses one *function* twice. A common combination is with [Table](map.md#table), `⌜˜`, so that the operand function is called on each combination of elements in `𝕩` to form a square result. For example, `=⌜˜` applied to `↕n` gives the identity matrix of size `n`. ט 4 =⌜˜ ↕3 -Note that Self isn't needed with Before (`⊸`) and After (`⟜`), which essentially have a copy built in: for example `F⊸G 𝕩` is the same as `F⊸G˜ 𝕩` by definition. +Note that Self isn't needed with [Before](hook.md) (`⊸`) [and After](hook.md) (`⟜`), which essentially have a copy built in: for example `F⊸G 𝕩` is the same as `F⊸G˜ 𝕩` by definition. diff --git a/doc/syntax.md b/doc/syntax.md index a21752ca..7464d533 100644 --- a/doc/syntax.md +++ b/doc/syntax.md @@ -2,11 +2,11 @@ # Syntax overview -BQN syntax consists of expressions where computation is done with a little organizing structure around them like assignment, functions, and list notation. Expressions are where the programmer is in control so the design tries to do as much as possible with them before introducing special syntax. +BQN syntax consists of expressions where computation is done, with a little organizing structure around them like assignment, functions, and list notation. Expressions are where the programmer is in control, so the design tries to do as much as possible with them before introducing special syntax. ## Special glyphs -The following glyphs are used for BQN syntax. [Primitives](primitive.md) (built-in functions and modifiers) are not listed in this table, and have their own page. Digits, characters, and the underscore `_` are used for numbers, and identifiers or variable names. +The following glyphs are used for BQN syntax. [Primitives](primitive.md) (built-in functions and modifiers) are not listed in this table, and have their own page. Digits, characters, and the underscore `_` are used for numbers and variable names. Glyph(s) | Meaning ----------------|----------- @@ -35,29 +35,31 @@ Glyph(s) | Meaning ## Comments -A comment starts with `#` that is not part of a string and continues to the end of the line. +A comment starts with a `#` that isn't part of a character or string literal, and continues to the end of the line. + + '#' - 1 #This is the comment ## Constants BQN has single-token notation for numbers, strings, and characters. -Numbers allow the typical decimal notation with `¯` for the negative sign (because `-` is a function) and `e` for scientific notation (or `E`, as numeric notation is case-insensitive). `∞` and `π` may be used as special numeric values. If complex numbers are supported, then they can be written with the components separated by `i`. However, no BQN to date supports complex numbers. +[Numbers](types.md#numbers) are written as decimals, allowing `¯` for the negative sign (because `-` is a function) and `e` or `E` for scientific notation. They must have digits before and after the decimal point (so, `0.5` instead of `.5`), and any exponent must be an integer. Two special numbers `∞` and `π` are supported, possibly with a minus sign. If complex numbers are supported (no implementation to date has them), then they can be written with the components separated by `i` or `I`. ⟨ ¯π ⋄ 0.5 ⋄ 5e¯1 ⋄ 1.5E3 ⋄ ∞ ⟩ # A list of numbers -Strings are written with double quotes `""`, and characters with single quotes `''` with a single character in between. A double quote within a string can be escaped by writing it twice; if two string literals are next to each other, they must be separated by a space. In contrast, character literals do not use escapes, as the length is already known. +Strings—lists of characters—are written with double quotes `""`, and [characters](types.md#characters) with single quotes `''` with a single character in between. Only one character ever needs to be escaped: a double quote in a string is written twice. So `""""` is a one-character string of `"`, and if two string literals are next to each other, they have to be separated by a space. Character literals don't have even one escape, as the length is already known. Other than the double quote, character and string literals can contain anything: newlines, null characters, or any other Unicode. ≠¨ ⟨ "str" ⋄ "s't""r" ⋄ 'c' ⋄ ''' ⋄ '"' ⟩ # "" is an escape ≡¨ ⟨ "a" ⋄ 'a' ⟩ # A string is an array but a character isn't -The null character (code point 0) has a dedicated literal representation `@`. This character can be used to directly convert between characters and numeric code points, which among many other uses allows tricky characters to be entered by code point: for example, a non-breaking space is `@+160`. The character can also be entered as a character literal, but this will display differently in various editors and some tools may have trouble with a file directly containing a null, so it is best to use `@` instead. +But including a null character in your source code is probably not a great idea for other reasons. The null character (code point 0) has a dedicated literal representation `@`. Null can be used with [character arithmetic](arithmetic.md#character-arithmetic) to directly convert between characters and numeric code points, which among many other uses allows tricky characters to be entered by code point: for example, a non-breaking space is `@+160`. ## Expressions *[Full documentation](expression.md)* -BQN expressions are composed of subjects, functions, and modifiers, with parentheses to group parts into subexpressions. Functions can be applied to subjects or grouped into trains, while modifiers can be applied to subjects or functions. The most important kinds of application are: +BQN expressions are composed of subjects, functions, and modifiers, with parentheses to group parts into subexpressions. [Functions](ops.md#functions) can be applied to subjects or grouped into [trains](train.md), while [modifiers](ops.md#modifiers) can be applied to subjects or functions. The most important kinds of application are: | example | left | main | right | output | name | binding |---------|-------|-------|-------|------------|------------|--------- @@ -66,30 +68,30 @@ BQN expressions are composed of subjects, functions, and modifiers, with parenth | `×´` | `F` | `_m` | | Function | 1-Modifier | LtR, tighter | `2⊸\|` | `F` | `_c_` | `G` | Function | 2-Modifier | -The four roles (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's [type](types.md) doesn't have to correspond to its role, and can even change from one evaluation to another. An expression's role is determined entirely by its source code, so it's fixed. +The four [roles](expression.md#syntactic-role) (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's [type](types.md) doesn't have to correspond to its role, and can even change from one evaluation to another. An expression's role is determined entirely by its source code, so it's fixed. -Assignment arrows `←`, `↩`, and `⇐` store expression results in variables: `←` and `⇐` create new variables while `↩` modifies existing ones. The general format is `Name ← Value`, where the two sides have the same role. Additionally, `lhs F↩ rhs` is a shortened form of `lhs ↩ lhs F rhs` and `lhs F↩` expands to `lhs ↩ F lhs`. +[Assignment](expression.md#assignment) arrows `←`, `↩`, and `⇐` store expression results in variables: `←` and `⇐` create new variables while `↩` modifies existing ones. The general format is `Name ← Value`, where the two sides have the same role. Additionally, `lhs F↩ rhs` is a shortened form of `lhs ↩ lhs F rhs` and `lhs F↩` expands to `lhs ↩ F lhs`. -The double arrow `⇐` is used for functionality relating to [namespaces](namespace.md). It has a few purposes: exporting assignment `name⇐value`, plain export `name⇐`, and aliasing `⟨alias⇐field⟩←namespace`. A block that uses it for export returns a namespace rather than the result of its last statement. +The double arrow `⇐` is used for functionality relating to [namespaces](namespace.md). It has a few purposes: exporting assignment `name⇐value`, plain export `name⇐`, and aliasing `⟨alias⇐field⟩←namespace`. A block that uses it for export returns a namespace rather than the result of its last statement. The other namespace-related bit of syntax is field access `ns.field`. ## Lists and blocks +Lists and code blocks can both be represented as sequences of expressions in source code. They both have paired bracket representations, using `⟨⟩` for lists and `{}` for blocks, as well as a shortcut "stranding" notation using `‿` for lists. + ### Separators -The characters `⋄` and `,` and newline are completely interchangeable and are used to separate expressions. An expression might be an element in a list or a line in a function. Empty sections—those that consist only of whitespace—are ignored. This means that any number of separators can be used between expressions, and that leading and trailing separators are also allowed. The expressions are evaluated in text order: left to right and top to bottom. +The characters `⋄` and `,` and newline are completely interchangeable and are used to separate expressions. An expression might be an element in a list or a line in a block. Empty sections—those that consist only of whitespace—are ignored. This means that any number of separators can be used between expressions, and that leading and trailing separators are also allowed. The expressions are evaluated in text order: left to right and top to bottom. ### List notation *[Full documentation](arrayrepr.md#list-literals)* -Lists (1-dimensional arrays) are enclosed in angle brackets `⟨⟩`, with the results of the expressions in between being the list's elements. Lists of two elements or more can also be written with the ligature character `‿`. This character has higher binding strength than any part of an expression. If one of the elements is a compound expression, then it will need to be enclosed in parentheses. +Lists (1-dimensional arrays) are enclosed in angle brackets `⟨⟩`, with the results of the expressions in between being the list's elements. Lists of two elements or more can also be written with the ligature character `‿`. This character has higher binding strength than any part of an expression except `.` for namespace field access. If one of the elements is a compound expression, then it will need to be enclosed in parentheses. ### Blocks *[Full documentation](block.md)* -Blocks are written with curly braces `{}` and can be used to group expressions or define functions and modifiers. The contents are simply a sequence of expressions, where each is evaluated and the result of the last is returned in order to evaluate the block. This result can have any value, and its syntactic role in the calling context is determined by the normal rules: functions return subjects and modifiers return functions. Blocks have [lexical scope](lexical.md). - -The special names `𝕨` and `𝕩`, which stand for arguments, and `𝕗` and `𝕘`, which stand for operands, are available inside curly braces. Like ordinary names, the lowercase forms indicate subjects and the uppercase forms `𝕎𝕏𝔽𝔾` indicate functions. The type and syntactic role of the block is determined by its contents: a 2-modifier contains `𝕘`, a 1-modifier contains `𝕗` but not `𝕘`, and a function contains neither but does have one of `𝕨𝕩𝕤𝕎𝕏𝕊`. If no special names are present the block is an *immediate block* and is evaluated as soon as it appears, with the result having a subject role. +Blocks are written with curly braces `{}` and can have a subject, function, or modifier role. The contents are any number of bodies separated by `;`. Each body is a sequence of expressions to be evaluated in order, possibly with a header, followed by `:`, that sets the type and describes expected inputs. A body runs in its own environment according to the rules of [lexical scoping](lexical.md). The result is either a [namespace](namespace.md), if the body used `⇐`, or the result of the last expression. -A modifier can be evaluated twice: once when passed operands and again when the resulting function is passed arguments. If it contains `𝕨` or `𝕩`, the first evaluation simply remembers the operands, and the contents will be executed only on the second evaluation, when the arguments are available. If it doesn't contain these, then the contents are executed on the first evaluation and the result is treated as a function. +The special names `𝕨` and `𝕩`, which stand for arguments, and `𝕗` and `𝕘`, which stand for operands, are available inside curly braces. Like ordinary names, the lowercase forms indicate subjects and the uppercase forms `𝕎𝕏𝔽𝔾` indicate functions. If it has no header, the type and syntactic role of the block is determined by its contents: a 2-modifier contains `𝕘`, a 1-modifier contains `𝕗` but not `𝕘`, and a function contains neither but does have one of `𝕨𝕩𝕤𝕎𝕏𝕊`. The last option is an immediate block, which has a subject role and runs as soon as it's encountered. diff --git a/doc/types.md b/doc/types.md index af6eaa65..27b31f89 100644 --- a/doc/types.md +++ b/doc/types.md @@ -67,7 +67,7 @@ Other linear combinations such as adding two characters or negating a character ### Arrays -*Full documentation [here](array.md).* +*[Full documentation](array.md).* A BQN array is a multidimensional arrangement of data. This means it has a certain [*shape*](shape.md), which is a finite list of natural numbers giving the length along each axis, and it contains an *element* for each possible [*index*](indices.md), which is a choice of one natural number that's less than each axis length in the shape. The total number of elements, or *bound*, is then the product of all the lengths in the shape. The shape may have any length including zero, and this shape is known as the array's *rank*. An array of rank 0, which always contains exactly one element, is called a *unit*, while an array of rank 1 is called a *list* and an array of rank 2 is called a *table*. @@ -79,7 +79,7 @@ Different elements of an array should not influence each other. While some APLs ## Operation types -*Full documentation [here](ops.md).* +*[Full documentation](ops.md).* An operation is either a function or modifier, and can be applied to *inputs*—which are called *arguments* for functions and *operands* for modifiers—to obtain a result. During this application an operation might also change variables within its scope and call other operations, or cause an error, in which case it doesn't return a result. There is one type of call for each of the three operation types, and an operation will give an error if it is called in a way that doesn't match its type. @@ -95,6 +95,6 @@ A 1-modifier is called with one operand, while a 2-modifier is called with two. ## Namespaces -*Full documentation [here](namespace.md).* +*[Full documentation](namespace.md).* Functions and modifiers have internal scopes which they can manipulate (by defining and modifying variables) to save and update information. Namespaces let the programmer to expose this state more directly: identifiers in a namespace may be exported, allowing code outside the namespace to read their values. diff --git a/docs/doc/shift.html b/docs/doc/shift.html index 5b72e865..8e8b3640 100644 --- a/docs/doc/shift.html +++ b/docs/doc/shift.html @@ -5,23 +5,25 @@ </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="shift-functions"><a class="header" href="#shift-functions">Shift functions</a></h1> -<p>The shift functions <code><span class='Function'>«</span></code> and <code><span class='Function'>»</span></code> add major cells to one side an array, displacing cells on the opposite side and moving those in between. Shifts resemble but are more general than the bit-based shift operations used in low-level languages. They replace the APL pattern of a 2-wise reduction after appending or prepending an element (APL's <code><span class='Number'>2</span><span class='Function'>≠/</span><span class='Number'>0</span><span class='Separator'>,</span><span class='Value'>v</span></code> translates to <code><span class='Function'>»</span><span class='Modifier2'>⊸</span><span class='Function'>≠</span><span class='Value'>v</span></code>), one of the more common uses of 2-wise reduction.</p> +<p>The shift functions <code><span class='Function'>«</span></code> and <code><span class='Function'>»</span></code> add <a href="array.html#cells">major cells</a> to one side an array, displacing cells on the opposite side and moving those in between. Shifts resemble but are more general than the bit-based shift operations used in low-level languages. They replace the APL pattern of a 2-wise reduction after appending or prepending an element (APL's <code><span class='Number'>2</span><span class='Function'>≠/</span><span class='Number'>0</span><span class='Separator'>,</span><span class='Value'>v</span></code> translates to <code><span class='Function'>»</span><span class='Modifier2'>⊸</span><span class='Function'>≠</span><span class='Value'>v</span></code>); a nice version of this common pattern is one reason BQN is free to replace windowed reduction with the sometimes less convenient <a href="windows.html">Windows</a>.</p> <p>The result of a shift function always has the same shape as <code><span class='Value'>𝕩</span></code>. The function adds major cells to the beginning (<code><span class='Function'>»</span></code>) or end (<code><span class='Function'>«</span></code>) of <code><span class='Value'>𝕩</span></code>, moving the cells already in <code><span class='Value'>𝕩</span></code> to accomodate them. Some cells on the opposite side from those added will "fall off" and not be included in the result.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MOKAvzAgwrsgM+KAvzLigL8xICAgICAgICAgICAgICMgU2hpZnQgQmVmb3JlCiJlbmQiIMKrICJhZGQgdG8gdGhlICIgICAjIFNoaWZ0IEFmdGVy">↗️</a><pre> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span> <span class='Function'>»</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span> <span class='Comment'># Shift Before +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MOKAvzAgwrsgM+KAvzLigL8xICAgICAgICAgICAgICMgU2hpZnQgQmVmb3JlCgoiZW5kIiDCqyAiYWRkIHRvIHRoZSAiICAgIyBTaGlmdCBBZnRlcg==">↗️</a><pre> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span> <span class='Function'>»</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span> <span class='Comment'># Shift Before </span>⟨ 0 0 3 ⟩ + <span class='String'>"end"</span> <span class='Function'>«</span> <span class='String'>"add to the "</span> <span class='Comment'># Shift After </span>" to the end" </pre> <p>The cells to add come from <code><span class='Value'>𝕨</span></code> if it's present, as shown above. Otherwise, a single cell of <a href="fill.html">fill elements</a> for <code><span class='Value'>𝕩</span></code> is used. This kind of shift, which moves cells in <code><span class='Value'>𝕩</span></code> over by just one, is called a "nudge".</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=wrsgImFiY2QiICAgIyBOdWRnZQrCqyAx4oC/MuKAvzMgICAgIyBOdWRnZSBCYWNr">↗️</a><pre> <span class='Function'>»</span> <span class='String'>"abcd"</span> <span class='Comment'># Nudge +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=wrsgImFiY2QiICAgIyBOdWRnZQoKwqsgMeKAvzLigL8zICAgICMgTnVkZ2UgQmFjaw==">↗️</a><pre> <span class='Function'>»</span> <span class='String'>"abcd"</span> <span class='Comment'># Nudge </span>" abc" + <span class='Function'>«</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span> <span class='Comment'># Nudge Back </span>⟨ 2 3 0 ⟩ </pre> -<p>If <code><span class='Value'>𝕨</span></code> is longer than <code><span class='Value'>𝕩</span></code>, some cells from <code><span class='Value'>𝕨</span></code> will be discarded, as well as all of <code><span class='Value'>𝕩</span></code>. In this case <code><span class='Value'>𝕨</span><span class='Function'>»</span><span class='Value'>𝕩</span></code> is <code><span class='Paren'>(</span><span class='Function'>≠</span><span class='Value'>𝕩</span><span class='Paren'>)</span><span class='Function'>↑</span><span class='Value'>𝕨</span></code> and <code><span class='Value'>𝕨</span><span class='Function'>«</span><span class='Value'>𝕩</span></code> is <code><span class='Paren'>(</span><span class='Function'>-≠</span><span class='Value'>𝕩</span><span class='Paren'>)</span><span class='Function'>↑</span><span class='Value'>𝕨</span></code>. For similar reasons, nudging an array of length 0 returns it unchanged.</p> +<p>If <code><span class='Value'>𝕨</span></code> is longer than <code><span class='Value'>𝕩</span></code>, some cells from <code><span class='Value'>𝕨</span></code> will be discarded, plus all of <code><span class='Value'>𝕩</span></code>. In this case <code><span class='Value'>𝕨</span><span class='Function'>»</span><span class='Value'>𝕩</span></code> is <code><span class='Paren'>(</span><span class='Function'>≠</span><span class='Value'>𝕩</span><span class='Paren'>)</span><span class='Function'>↑</span><span class='Value'>𝕨</span></code> and <code><span class='Value'>𝕨</span><span class='Function'>«</span><span class='Value'>𝕩</span></code> is <code><span class='Paren'>(</span><span class='Function'>-≠</span><span class='Value'>𝕩</span><span class='Paren'>)</span><span class='Function'>↑</span><span class='Value'>𝕨</span></code>. For similar reasons, nudging an array of length 0 returns it unchanged.</p> <h2 id="sequence-processing-with-shifts"><a class="header" href="#sequence-processing-with-shifts">Sequence processing with shifts</a></h2> <p>When working with a sequence of data such as text, daily measurements, or audio data, shift functions are generally the best way to handle the concept of "next" or "previous". In the following example <code><span class='Value'>s</span></code> is shown alongside the shifted-right data <code><span class='Function'>»</span><span class='Value'>s</span></code>, and each element is compared to the previous with <code><span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Function'>»</span></code>, which we see is the <a href="undo.html">inverse</a> of Plus <a href="scan.html">Scan</a> <code><span class='Function'>+</span><span class='Modifier'>`</span></code>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cyDihpAgMeKAvzLigL8y4oC/NOKAvzPigL814oC/NgpzIOKJjSDCu3MKLeKfnMK7IHMKCitgIC3in5zCuyBz">↗️</a><pre> <span class='Value'>s</span> <span class='Gets'>←</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>5</span><span class='Ligature'>‿</span><span class='Number'>6</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cyDihpAgMeKAvzLigL8y4oC/NOKAvzPigL814oC/NgpzIOKJjSDCu3MKLeKfnMK7IHMKCitgIC3in5zCuyBzICAgIyBTYW1lIGFzIHM=">↗️</a><pre> <span class='Value'>s</span> <span class='Gets'>←</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>5</span><span class='Ligature'>‿</span><span class='Number'>6</span> <span class='Value'>s</span> <span class='Function'>≍</span> <span class='Function'>»</span><span class='Value'>s</span> ┌─ ╵ 1 2 2 4 3 5 6 @@ -30,8 +32,8 @@ <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Function'>»</span> <span class='Value'>s</span> ⟨ 1 1 0 2 ¯1 2 1 ⟩ - <span class='Function'>+</span><span class='Modifier'>`</span> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Function'>»</span> <span class='Value'>s</span> -⟨ 1 2 2 4 3 5 6 ⟩ + <span class='Function'>+</span><span class='Modifier'>`</span> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Function'>»</span> <span class='Value'>s</span> <span class='Comment'># Same as s +</span>⟨ 1 2 2 4 3 5 6 ⟩ </pre> <p>In this way <code><span class='Function'>»</span></code> refers to a sequence containing the previous element at each position. By default the array's fill is used for the element before the first, and a right argument can be given to provide a different one.</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oieIMK7IHMKCuKKj+KKuMK7IHM=">↗️</a><pre> <span class='Number'>∞</span> <span class='Function'>»</span> <span class='Value'>s</span> @@ -59,7 +61,7 @@ </pre> <p>A feature these examples all share is that they maintain the length of <code><span class='Value'>s</span></code>. This is a common condition in sequence processing, particularly when the processed sequence needs to be combined or compared with the original in some way. However, it's not always the case. In some instances, for example when searching <code><span class='Value'>s</span></code> to see if there is any value less than the previous, the list should get shorter during processing. In these cases, <a href="windows.html">Windows</a> (<code><span class='Function'>↕</span></code>) is usually a better choice.</p> <h2 id="arithmetic-and-logical-shifts"><a class="header" href="#arithmetic-and-logical-shifts">Arithmetic and logical shifts</a></h2> -<p>The glyphs <code><span class='Function'>«</span></code> and <code><span class='Function'>»</span></code>, suggesting movement, were chosen for the same reasons as the digraphs <code><span class='Function'><<</span></code> and <code><span class='Function'>>></span></code>, and can be used to implement the same operations on boolean lists.</p> +<p>The glyphs <code><span class='Function'>«</span></code> and <code><span class='Function'>»</span></code>, suggesting movement, were chosen for the same reasons as the digraphs <code><span class='Function'><<</span></code> and <code><span class='Function'>>></span></code> in C-like languages, and can be used to implement the same bit-shift operations on boolean lists.</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGkg4oaQICIxMDAxMTAxMSItJzAnCgrCq+KNnzMgaSAgICAgICAgIyBRdWljayBhbmQgZGlydHkgbGVmdCBzaGlmdAoKMyDipYrin5ww4oq4wqsgaSAgICAjIEFsdGVybmF0ZSBsZWZ0IHNoaWZ0">↗️</a><pre> <span class='Function'>⊢</span> <span class='Value'>i</span> <span class='Gets'>←</span> <span class='String'>"10011011"</span><span class='Function'>-</span><span class='String'>'0'</span> ⟨ 1 0 0 1 1 0 1 1 ⟩ @@ -77,7 +79,7 @@ </span>⟨ 1 1 1 1 0 0 1 1 ⟩ </pre> <h2 id="other-examples"><a class="header" href="#other-examples">Other examples</a></h2> -<p>In <a href="take.html">Take</a> (<code><span class='Function'>↑</span></code>), there's no way to specify the fill element when the result is longer than the argument. To take along the first axis with a specified, constant fill value, you can use Shift Before instead, where the right argument is an array of fills with the desired final shape.</p> +<p>In <a href="take.html">Take</a> (<code><span class='Function'>↑</span></code>), there's no way to specify the fill element when the result is longer than the argument. To take along the first axis with a specified, constant fill value, you can use Shift Before instead, where the right argument is an array of fills with the desired final shape (a more general approach is <a href="under.html">Under</a>).</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ImFiYyIgwrsgNeKliidGJw==">↗️</a><pre> <span class='String'>"abc"</span> <span class='Function'>»</span> <span class='Number'>5</span><span class='Function'>⥊</span><span class='String'>'F'</span> "abcFF" </pre> @@ -91,12 +93,12 @@ <span class='Number'>2</span> <span class='Function'>+</span><span class='Modifier'>`</span><span class='Modifier2'>∘</span><span class='Function'>»</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span> <span class='Comment'># Final value not created </span>⟨ 2 3 3 4 ⟩ </pre> -<p>The <em>strides</em> of an array are the distances between one element and the next along any given axis. It's the product of all axis lengths below that axis, since these are all the axes that have to be "skipped" to jump along the axis. The strides of an array <code><span class='Value'>𝕩</span></code> are <code><span class='Paren'>(</span><span class='Function'>×</span><span class='Modifier'>`</span><span class='Number'>1</span><span class='Function'>»⊢</span><span class='Paren'>)</span><span class='Modifier2'>⌾</span><span class='Function'>⌽</span><span class='Modifier2'>∘</span><span class='Function'>≢</span><span class='Value'>𝕩</span></code>.</p> +<p>The <em>strides</em> of an array are the distances between one element and the next along any given axis. It's the product of all axis lengths below that axis, since these are all the axes that have to be "skipped" to jump along the axis. The strides of an array <code><span class='Value'>𝕩</span></code> are <code><span class='Paren'>(</span><span class='Function'>×</span><span class='Modifier'>`</span><span class='Number'>1</span><span class='Function'>»⊢</span><span class='Paren'>)</span><span class='Modifier2'>⌾</span><span class='Function'>⌽</span> <span class='Function'>≢</span><span class='Value'>𝕩</span></code>.</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KMOXYDHCu+KKoinijL7ijL0gNeKAvzLigL804oC/Mw==">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>×</span><span class='Modifier'>`</span><span class='Number'>1</span><span class='Function'>»⊢</span><span class='Paren'>)</span><span class='Modifier2'>⌾</span><span class='Function'>⌽</span> <span class='Number'>5</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>3</span> ⟨ 24 12 3 1 ⟩ </pre> <h2 id="higher-rank"><a class="header" href="#higher-rank">Higher rank</a></h2> -<p>Shifting always works on the first axis of <code><span class='Value'>𝕩</span></code> (which must have rank 1 or more), and shifts in major cells. A left argument can have rank equal to <code><span class='Value'>𝕩</span></code>, or one less than it, in which case it becomes a single cell of the result. With no left argument, a cell of fills <code><span class='Number'>1</span><span class='Function'>↑</span><span class='Number'>0</span><span class='Function'>↑</span><span class='Value'>𝕩</span></code> is nudged in.</p> +<p>Shifting always works on the <a href="leading.html">first axis</a> of <code><span class='Value'>𝕩</span></code> (which must have rank 1 or more), and shifts in major cells. A left argument can have rank equal to <code><span class='Value'>𝕩</span></code>, or one less than it, in which case it becomes a single cell of the result. With no left argument, a cell of fills <code><span class='Number'>1</span><span class='Function'>↑</span><span class='Number'>0</span><span class='Function'>↑</span><span class='Value'>𝕩</span></code> is nudged in.</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQIOKliuKfnCjihpXDl8K0KSA04oC/MwoKwrsgYSAgICAgICAgICAgICAgICAjIE51ZGdlIGFkZHMgYSBjZWxsIG9mIGZpbGxzCgoib25lIiDCqyBhICAgICAgICAgICMgU2hpZnQgaW4gYSBjZWxsCgooInR3byLiiY0iY2VsIikgwqsgYSAgIyBTaGlmdCBpbiBtdWx0aXBsZSBjZWxscw==">↗️</a><pre> <span class='Function'>⊢</span> <span class='Value'>a</span> <span class='Gets'>←</span> <span class='Function'>⥊</span><span class='Modifier2'>⟜</span><span class='Paren'>(</span><span class='Function'>↕×</span><span class='Modifier'>´</span><span class='Paren'>)</span> <span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>3</span> ┌─ ╵ 0 1 2 diff --git a/docs/doc/swap.html b/docs/doc/swap.html index 190b0d0a..409de900 100644 --- a/docs/doc/swap.html +++ b/docs/doc/swap.html @@ -54,8 +54,8 @@ </tr> </tbody> </table> -<p>Since <code><span class='Value'>𝕩</span></code> is always the left argument, these two definitions can be unified as <code><span class='Brace'>{</span><span class='Value'>𝕩</span><span class='Function'>𝔽</span><span class='Value'>𝕨</span><span class='Function'>⊣</span><span class='Value'>𝕩</span><span class='Brace'>}</span></code>, noting that <a href="identity.html">Left</a> becomes a plain identity function when the left argument <code><span class='Value'>𝕨</span></code> isn't given.</p> -<p>Swap is arguably less transformative. Some common examples are <code><span class='Function'>-</span><span class='Modifier'>˜</span></code> and <code><span class='Function'>÷</span><span class='Modifier'>˜</span></code>, since these two functions run the <a href="../commentary/problems.html#subtraction-division-and-span-are-backwards">wrong way</a> for BQN's evaluation order. This is very often useful in <a href="tacit.html">tacit</a> programming, and less useful for explicit code. While it sometimes allows for shorter code by making a pair of parentheses unnecessary (say, <code><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>b</span><span class='Paren'>)</span><span class='Function'>-</span><span class='Value'>c</span></code> is <code><span class='Value'>c</span><span class='Function'>-</span><span class='Modifier'>˜</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>b</span></code>), I personally don't think this is always a good idea. My opinion is that it should be used when it makes the semantics a better fit for BQN, but putting the primary argument on the right and a secondary or control argument on the left.</p> +<p>Since <code><span class='Value'>𝕩</span></code> always becomes the left argument, these two definitions can be unified as <code><span class='Brace'>{</span><span class='Value'>𝕩</span><span class='Function'>𝔽</span><span class='Value'>𝕨</span><span class='Function'>⊣</span><span class='Value'>𝕩</span><span class='Brace'>}</span></code>, noting that <a href="identity.html">Left</a> returns <code><span class='Value'>𝕨</span></code> if it's given and <code><span class='Value'>𝕩</span></code> if not.</p> +<p>Swap is arguably less transformative. Some common examples are <code><span class='Function'>-</span><span class='Modifier'>˜</span></code> and <code><span class='Function'>÷</span><span class='Modifier'>˜</span></code>, since these two functions run the <a href="../commentary/problems.html#subtraction-division-and-span-are-backwards">wrong way</a> for BQN's evaluation order. This is very often useful in <a href="tacit.html">tacit</a> programming, and less needed for explicit code. While it sometimes allows for shorter code by making a pair of parentheses unnecessary (say, <code><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>b</span><span class='Paren'>)</span><span class='Function'>-</span><span class='Value'>c</span></code> is <code><span class='Value'>c</span><span class='Function'>-</span><span class='Modifier'>˜</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>b</span></code>), I personally don't think this is always a good idea. My opinion is that it should be used when it makes the semantics a better fit for BQN, but putting the primary argument on the right and a secondary or control argument on the left.</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=J2EnIOKLiMucICdiJwoKIiArIiDiio/LnCAw4oC/MeKAvzHigL8w4oC/MOKJjTHigL8w4oC/MeKAvzDigL8x">↗️</a><pre> <span class='String'>'a'</span> <span class='Function'>⋈</span><span class='Modifier'>˜</span> <span class='String'>'b'</span> "ba" @@ -65,7 +65,7 @@ + + +" ┘ </pre> -<p>Self re-uses one argument twice. In this way it's a little like <a href="compose.html">Over</a>, which re-uses one <em>function</em> twice. A common combination is with Table, <code><span class='Modifier'>⌜˜</span></code>, so that the operand function is called on each combination of elements from the argument to form a square result. For example, <code><span class='Function'>=</span><span class='Modifier'>⌜˜</span></code> applied to <code><span class='Function'>↕</span><span class='Value'>n</span></code> gives the identity matrix of size <code><span class='Value'>n</span></code>.</p> +<p>Moving on, Self re-uses one argument twice. In this way it's a little like <a href="compose.html">Over</a>, which re-uses one <em>function</em> twice. A common combination is with <a href="map.html#table">Table</a>, <code><span class='Modifier'>⌜˜</span></code>, so that the operand function is called on each combination of elements in <code><span class='Value'>𝕩</span></code> to form a square result. For example, <code><span class='Function'>=</span><span class='Modifier'>⌜˜</span></code> applied to <code><span class='Function'>↕</span><span class='Value'>n</span></code> gives the identity matrix of size <code><span class='Value'>n</span></code>.</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=w5fLnCA0Cgo94oycy5wg4oaVMw==">↗️</a><pre> <span class='Function'>×</span><span class='Modifier'>˜</span> <span class='Number'>4</span> 16 @@ -76,4 +76,4 @@ 0 0 1 ┘ </pre> -<p>Note that Self isn't needed with Before (<code><span class='Modifier2'>⊸</span></code>) and After (<code><span class='Modifier2'>⟜</span></code>), which essentially have a copy built in: for example <code><span class='Function'>F</span><span class='Modifier2'>⊸</span><span class='Function'>G</span> <span class='Value'>𝕩</span></code> is the same as <code><span class='Function'>F</span><span class='Modifier2'>⊸</span><span class='Function'>G</span><span class='Modifier'>˜</span> <span class='Value'>𝕩</span></code> by definition.</p> +<p>Note that Self isn't needed with <a href="hook.html">Before</a> (<code><span class='Modifier2'>⊸</span></code>) <a href="hook.html">and After</a> (<code><span class='Modifier2'>⟜</span></code>), which essentially have a copy built in: for example <code><span class='Function'>F</span><span class='Modifier2'>⊸</span><span class='Function'>G</span> <span class='Value'>𝕩</span></code> is the same as <code><span class='Function'>F</span><span class='Modifier2'>⊸</span><span class='Function'>G</span><span class='Modifier'>˜</span> <span class='Value'>𝕩</span></code> by definition.</p> diff --git a/docs/doc/syntax.html b/docs/doc/syntax.html index 8798c1e4..6312fbbd 100644 --- a/docs/doc/syntax.html +++ b/docs/doc/syntax.html @@ -5,9 +5,9 @@ </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="syntax-overview"><a class="header" href="#syntax-overview">Syntax overview</a></h1> -<p>BQN syntax consists of expressions where computation is done with a little organizing structure around them like assignment, functions, and list notation. Expressions are where the programmer is in control so the design tries to do as much as possible with them before introducing special syntax.</p> +<p>BQN syntax consists of expressions where computation is done, with a little organizing structure around them like assignment, functions, and list notation. Expressions are where the programmer is in control, so the design tries to do as much as possible with them before introducing special syntax.</p> <h2 id="special-glyphs"><a class="header" href="#special-glyphs">Special glyphs</a></h2> -<p>The following glyphs are used for BQN syntax. <a href="primitive.html">Primitives</a> (built-in functions and modifiers) are not listed in this table, and have their own page. Digits, characters, and the underscore <code><span class='Modifier2'>_</span></code> are used for numbers, and identifiers or variable names.</p> +<p>The following glyphs are used for BQN syntax. <a href="primitive.html">Primitives</a> (built-in functions and modifiers) are not listed in this table, and have their own page. Digits, characters, and the underscore <code><span class='Modifier2'>_</span></code> are used for numbers and variable names.</p> <table> <thead> <tr> @@ -107,24 +107,27 @@ </tbody> </table> <h2 id="comments"><a class="header" href="#comments">Comments</a></h2> -<p>A comment starts with <code><span class='Comment'>#</span></code> that is not part of a string and continues to the end of the line.</p> +<p>A comment starts with a <code><span class='Comment'>#</span></code> that isn't part of a character or string literal, and continues to the end of the line.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=JyMnIC0gMSAgI1RoaXMgaXMgdGhlIGNvbW1lbnQ=">↗️</a><pre> <span class='String'>'#'</span> <span class='Function'>-</span> <span class='Number'>1</span> <span class='Comment'>#This is the comment +</span>'"' +</pre> <h2 id="constants"><a class="header" href="#constants">Constants</a></h2> <p>BQN has single-token notation for numbers, strings, and characters.</p> -<p>Numbers allow the typical decimal notation with <code><span class='Number'>¯</span></code> for the negative sign (because <code><span class='Function'>-</span></code> is a function) and <code><span class='Value'>e</span></code> for scientific notation (or <code><span class='Function'>E</span></code>, as numeric notation is case-insensitive). <code><span class='Number'>∞</span></code> and <code><span class='Number'>π</span></code> may be used as special numeric values. If complex numbers are supported, then they can be written with the components separated by <code><span class='Value'>i</span></code>. However, no BQN to date supports complex numbers.</p> +<p><a href="types.html#numbers">Numbers</a> are written as decimals, allowing <code><span class='Number'>¯</span></code> for the negative sign (because <code><span class='Function'>-</span></code> is a function) and <code><span class='Value'>e</span></code> or <code><span class='Function'>E</span></code> for scientific notation. They must have digits before and after the decimal point (so, <code><span class='Number'>0.5</span></code> instead of <code><span class='Number'>.5</span></code>), and any exponent must be an integer. Two special numbers <code><span class='Number'>∞</span></code> and <code><span class='Number'>π</span></code> are supported, possibly with a minus sign. If complex numbers are supported (no implementation to date has them), then they can be written with the components separated by <code><span class='Value'>i</span></code> or <code><span class='Function'>I</span></code>.</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oIMKvz4Ag4ouEIDAuNSDii4QgNWXCrzEg4ouEIDEuNUUzIOKLhCDiiJ4g4p+pICAgIyBBIGxpc3Qgb2YgbnVtYmVycw==">↗️</a><pre> <span class='Bracket'>⟨</span> <span class='Number'>¯π</span> <span class='Separator'>⋄</span> <span class='Number'>0.5</span> <span class='Separator'>⋄</span> <span class='Number'>5e¯1</span> <span class='Separator'>⋄</span> <span class='Number'>1.5E3</span> <span class='Separator'>⋄</span> <span class='Number'>∞</span> <span class='Bracket'>⟩</span> <span class='Comment'># A list of numbers </span>⟨ ¯3.141592653589793 0.5 0.5 1500 ∞ ⟩ </pre> -<p>Strings are written with double quotes <code><span class='String'>""</span></code>, and characters with single quotes <code><span class='String'>''</span></code> with a single character in between. A double quote within a string can be escaped by writing it twice; if two string literals are next to each other, they must be separated by a space. In contrast, character literals do not use escapes, as the length is already known.</p> +<p>Strings—lists of characters—are written with double quotes <code><span class='String'>""</span></code>, and <a href="types.html#characters">characters</a> with single quotes <code><span class='String'>''</span></code> with a single character in between. Only one character ever needs to be escaped: a double quote in a string is written twice. So <code><span class='String'>""""</span></code> is a one-character string of <code><span class='String'>"</span></code>, and if two string literals are next to each other, they have to be separated by a space. Character literals don't have even one escape, as the length is already known. Other than the double quote, character and string literals can contain anything: newlines, null characters, or any other Unicode.</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omgwqgg4p+oICJzdHIiIOKLhCAicyd0IiJyIiDii4QgJ2MnIOKLhCAnJycg4ouEICciJyDin6kgICAjICIiIGlzIGFuIGVzY2FwZQoK4omhwqgg4p+oICJhIiDii4QgJ2EnIOKfqSAgICMgQSBzdHJpbmcgaXMgYW4gYXJyYXkgYnV0IGEgY2hhcmFjdGVyIGlzbid0">↗️</a><pre> <span class='Function'>≠</span><span class='Modifier'>¨</span> <span class='Bracket'>⟨</span> <span class='String'>"str"</span> <span class='Separator'>⋄</span> <span class='String'>"s't""r"</span> <span class='Separator'>⋄</span> <span class='String'>'c'</span> <span class='Separator'>⋄</span> <span class='String'>'''</span> <span class='Separator'>⋄</span> <span class='String'>'"'</span> <span class='Bracket'>⟩</span> <span class='Comment'># "" is an escape </span>⟨ 3 5 1 1 1 ⟩ <span class='Function'>≡</span><span class='Modifier'>¨</span> <span class='Bracket'>⟨</span> <span class='String'>"a"</span> <span class='Separator'>⋄</span> <span class='String'>'a'</span> <span class='Bracket'>⟩</span> <span class='Comment'># A string is an array but a character isn't </span>⟨ 1 0 ⟩ </pre> -<p>The null character (code point 0) has a dedicated literal representation <code><span class='String'>@</span></code>. This character can be used to directly convert between characters and numeric code points, which among many other uses allows tricky characters to be entered by code point: for example, a non-breaking space is <code><span class='String'>@</span><span class='Function'>+</span><span class='Number'>160</span></code>. The character can also be entered as a character literal, but this will display differently in various editors and some tools may have trouble with a file directly containing a null, so it is best to use <code><span class='String'>@</span></code> instead.</p> +<p>But including a null character in your source code is probably not a great idea for other reasons. The null character (code point 0) has a dedicated literal representation <code><span class='String'>@</span></code>. Null can be used with <a href="arithmetic.html#character-arithmetic">character arithmetic</a> to directly convert between characters and numeric code points, which among many other uses allows tricky characters to be entered by code point: for example, a non-breaking space is <code><span class='String'>@</span><span class='Function'>+</span><span class='Number'>160</span></code>.</p> <h2 id="expressions"><a class="header" href="#expressions">Expressions</a></h2> <p><em><a href="expression.html">Full documentation</a></em></p> -<p>BQN expressions are composed of subjects, functions, and modifiers, with parentheses to group parts into subexpressions. Functions can be applied to subjects or grouped into trains, while modifiers can be applied to subjects or functions. The most important kinds of application are:</p> +<p>BQN expressions are composed of subjects, functions, and modifiers, with parentheses to group parts into subexpressions. <a href="ops.html#functions">Functions</a> can be applied to subjects or grouped into <a href="train.html">trains</a>, while <a href="ops.html#modifiers">modifiers</a> can be applied to subjects or functions. The most important kinds of application are:</p> <table> <thead> <tr> @@ -176,17 +179,17 @@ </tr> </tbody> </table> -<p>The four roles (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's <a href="types.html">type</a> doesn't have to correspond to its role, and can even change from one evaluation to another. An expression's role is determined entirely by its source code, so it's fixed.</p> -<p>Assignment arrows <code><span class='Gets'>←</span></code>, <code><span class='Gets'>↩</span></code>, and <code><span class='Gets'>⇐</span></code> store expression results in variables: <code><span class='Gets'>←</span></code> and <code><span class='Gets'>⇐</span></code> create new variables while <code><span class='Gets'>↩</span></code> modifies existing ones. The general format is <code><span class='Function'>Name</span> <span class='Gets'>←</span> <span class='Function'>Value</span></code>, where the two sides have the same role. Additionally, <code><span class='Value'>lhs</span> <span class='Function'>F</span><span class='Gets'>↩</span> <span class='Value'>rhs</span></code> is a shortened form of <code><span class='Value'>lhs</span> <span class='Gets'>↩</span> <span class='Value'>lhs</span> <span class='Function'>F</span> <span class='Value'>rhs</span></code> and <code><span class='Value'>lhs</span> <span class='Function'>F</span><span class='Gets'>↩</span></code> expands to <code><span class='Value'>lhs</span> <span class='Gets'>↩</span> <span class='Function'>F</span> <span class='Value'>lhs</span></code>.</p> -<p>The double arrow <code><span class='Gets'>⇐</span></code> is used for functionality relating to <a href="namespace.html">namespaces</a>. It has a few purposes: exporting assignment <code><span class='Value'>name</span><span class='Gets'>⇐</span><span class='Value'>value</span></code>, plain export <code><span class='Value'>name</span><span class='Gets'>⇐</span></code>, and aliasing <code><span class='Bracket'>⟨</span><span class='Value'>alias</span><span class='Gets'>⇐</span><span class='Value'>field</span><span class='Bracket'>⟩</span><span class='Gets'>←</span><span class='Value'>namespace</span></code>. A block that uses it for export returns a namespace rather than the result of its last statement.</p> +<p>The four <a href="expression.html#syntactic-role">roles</a> (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's <a href="types.html">type</a> doesn't have to correspond to its role, and can even change from one evaluation to another. An expression's role is determined entirely by its source code, so it's fixed.</p> +<p><a href="expression.html#assignment">Assignment</a> arrows <code><span class='Gets'>←</span></code>, <code><span class='Gets'>↩</span></code>, and <code><span class='Gets'>⇐</span></code> store expression results in variables: <code><span class='Gets'>←</span></code> and <code><span class='Gets'>⇐</span></code> create new variables while <code><span class='Gets'>↩</span></code> modifies existing ones. The general format is <code><span class='Function'>Name</span> <span class='Gets'>←</span> <span class='Function'>Value</span></code>, where the two sides have the same role. Additionally, <code><span class='Value'>lhs</span> <span class='Function'>F</span><span class='Gets'>↩</span> <span class='Value'>rhs</span></code> is a shortened form of <code><span class='Value'>lhs</span> <span class='Gets'>↩</span> <span class='Value'>lhs</span> <span class='Function'>F</span> <span class='Value'>rhs</span></code> and <code><span class='Value'>lhs</span> <span class='Function'>F</span><span class='Gets'>↩</span></code> expands to <code><span class='Value'>lhs</span> <span class='Gets'>↩</span> <span class='Function'>F</span> <span class='Value'>lhs</span></code>.</p> +<p>The double arrow <code><span class='Gets'>⇐</span></code> is used for functionality relating to <a href="namespace.html">namespaces</a>. It has a few purposes: exporting assignment <code><span class='Value'>name</span><span class='Gets'>⇐</span><span class='Value'>value</span></code>, plain export <code><span class='Value'>name</span><span class='Gets'>⇐</span></code>, and aliasing <code><span class='Bracket'>⟨</span><span class='Value'>alias</span><span class='Gets'>⇐</span><span class='Value'>field</span><span class='Bracket'>⟩</span><span class='Gets'>←</span><span class='Value'>namespace</span></code>. A block that uses it for export returns a namespace rather than the result of its last statement. The other namespace-related bit of syntax is field access <code><span class='Value'>ns.field</span></code>.</p> <h2 id="lists-and-blocks"><a class="header" href="#lists-and-blocks">Lists and blocks</a></h2> +<p>Lists and code blocks can both be represented as sequences of expressions in source code. They both have paired bracket representations, using <code><span class='Bracket'>⟨⟩</span></code> for lists and <code><span class='Brace'>{}</span></code> for blocks, as well as a shortcut "stranding" notation using <code><span class='Ligature'>‿</span></code> for lists.</p> <h3 id="separators"><a class="header" href="#separators">Separators</a></h3> -<p>The characters <code><span class='Separator'>⋄</span></code> and <code><span class='Separator'>,</span></code> and newline are completely interchangeable and are used to separate expressions. An expression might be an element in a list or a line in a function. Empty sections—those that consist only of whitespace—are ignored. This means that any number of separators can be used between expressions, and that leading and trailing separators are also allowed. The expressions are evaluated in text order: left to right and top to bottom.</p> +<p>The characters <code><span class='Separator'>⋄</span></code> and <code><span class='Separator'>,</span></code> and newline are completely interchangeable and are used to separate expressions. An expression might be an element in a list or a line in a block. Empty sections—those that consist only of whitespace—are ignored. This means that any number of separators can be used between expressions, and that leading and trailing separators are also allowed. The expressions are evaluated in text order: left to right and top to bottom.</p> <h3 id="list-notation"><a class="header" href="#list-notation">List notation</a></h3> <p><em><a href="arrayrepr.html#list-literals">Full documentation</a></em></p> -<p>Lists (1-dimensional arrays) are enclosed in angle brackets <code><span class='Bracket'>⟨⟩</span></code>, with the results of the expressions in between being the list's elements. Lists of two elements or more can also be written with the ligature character <code><span class='Ligature'>‿</span></code>. This character has higher binding strength than any part of an expression. If one of the elements is a compound expression, then it will need to be enclosed in parentheses.</p> +<p>Lists (1-dimensional arrays) are enclosed in angle brackets <code><span class='Bracket'>⟨⟩</span></code>, with the results of the expressions in between being the list's elements. Lists of two elements or more can also be written with the ligature character <code><span class='Ligature'>‿</span></code>. This character has higher binding strength than any part of an expression except <code><span class='Value'>.</span></code> for namespace field access. If one of the elements is a compound expression, then it will need to be enclosed in parentheses.</p> <h3 id="blocks"><a class="header" href="#blocks">Blocks</a></h3> <p><em><a href="block.html">Full documentation</a></em></p> -<p>Blocks are written with curly braces <code><span class='Brace'>{}</span></code> and can be used to group expressions or define functions and modifiers. The contents are simply a sequence of expressions, where each is evaluated and the result of the last is returned in order to evaluate the block. This result can have any value, and its syntactic role in the calling context is determined by the normal rules: functions return subjects and modifiers return functions. Blocks have <a href="lexical.html">lexical scope</a>.</p> -<p>The special names <code><span class='Value'>𝕨</span></code> and <code><span class='Value'>𝕩</span></code>, which stand for arguments, and <code><span class='Value'>𝕗</span></code> and <code><span class='Value'>𝕘</span></code>, which stand for operands, are available inside curly braces. Like ordinary names, the lowercase forms indicate subjects and the uppercase forms <code><span class='Function'>𝕎𝕏𝔽𝔾</span></code> indicate functions. The type and syntactic role of the block is determined by its contents: a 2-modifier contains <code><span class='Value'>𝕘</span></code>, a 1-modifier contains <code><span class='Value'>𝕗</span></code> but not <code><span class='Value'>𝕘</span></code>, and a function contains neither but does have one of <code><span class='Value'>𝕨𝕩𝕤</span><span class='Function'>𝕎𝕏𝕊</span></code>. If no special names are present the block is an <em>immediate block</em> and is evaluated as soon as it appears, with the result having a subject role.</p> -<p>A modifier can be evaluated twice: once when passed operands and again when the resulting function is passed arguments. If it contains <code><span class='Value'>𝕨</span></code> or <code><span class='Value'>𝕩</span></code>, the first evaluation simply remembers the operands, and the contents will be executed only on the second evaluation, when the arguments are available. If it doesn't contain these, then the contents are executed on the first evaluation and the result is treated as a function.</p> +<p>Blocks are written with curly braces <code><span class='Brace'>{}</span></code> and can have a subject, function, or modifier role. The contents are any number of bodies separated by <code><span class='Head'>;</span></code>. Each body is a sequence of expressions to be evaluated in order, possibly with a header, followed by <code><span class='Head'>:</span></code>, that sets the type and describes expected inputs. A body runs in its own environment according to the rules of <a href="lexical.html">lexical scoping</a>. The result is either a <a href="namespace.html">namespace</a>, if the body used <code><span class='Gets'>⇐</span></code>, or the result of the last expression.</p> +<p>The special names <code><span class='Value'>𝕨</span></code> and <code><span class='Value'>𝕩</span></code>, which stand for arguments, and <code><span class='Value'>𝕗</span></code> and <code><span class='Value'>𝕘</span></code>, which stand for operands, are available inside curly braces. Like ordinary names, the lowercase forms indicate subjects and the uppercase forms <code><span class='Function'>𝕎𝕏𝔽𝔾</span></code> indicate functions. If it has no header, the type and syntactic role of the block is determined by its contents: a 2-modifier contains <code><span class='Value'>𝕘</span></code>, a 1-modifier contains <code><span class='Value'>𝕗</span></code> but not <code><span class='Value'>𝕘</span></code>, and a function contains neither but does have one of <code><span class='Value'>𝕨𝕩𝕤</span><span class='Function'>𝕎𝕏𝕊</span></code>. The last option is an immediate block, which has a subject role and runs as soon as it's encountered.</p> diff --git a/docs/doc/types.html b/docs/doc/types.html index c27a9400..430aae1e 100644 --- a/docs/doc/types.html +++ b/docs/doc/types.html @@ -57,13 +57,13 @@ </ul> <p>Other linear combinations such as adding two characters or negating a character are not allowed. You can check whether an application of <code><span class='Function'>+</span></code> or <code><span class='Function'>-</span></code> on numbers and characters is allowed by applying the same function to the "characterness" of each value: <code><span class='Number'>0</span></code> for a number and <code><span class='Number'>1</span></code> for a character. The result will be a number if this application gives <code><span class='Number'>0</span></code> and a character if this gives <code><span class='Number'>1</span></code>, and otherwise the operation is not allowed.</p> <h3 id="arrays"><a class="header" href="#arrays">Arrays</a></h3> -<p><em>Full documentation <a href="array.html">here</a>.</em></p> +<p><em><a href="array.html">Full documentation</a>.</em></p> <p>A BQN array is a multidimensional arrangement of data. This means it has a certain <a href="shape.html"><em>shape</em></a>, which is a finite list of natural numbers giving the length along each axis, and it contains an <em>element</em> for each possible <a href="indices.html"><em>index</em></a>, which is a choice of one natural number that's less than each axis length in the shape. The total number of elements, or <em>bound</em>, is then the product of all the lengths in the shape. The shape may have any length including zero, and this shape is known as the array's <em>rank</em>. An array of rank 0, which always contains exactly one element, is called a <em>unit</em>, while an array of rank 1 is called a <em>list</em> and an array of rank 2 is called a <em>table</em>.</p> <p>Each array—empty or nonempty—has an inferred property called a <a href="fill.html"><em>fill</em></a>. The fill either indicates what element should be used to pad an array, or that such an element is not known and an error should result. Fills can be used by <a href="take.html">Take</a> (<code><span class='Function'>↑</span></code>), the two <a href="shift.html">Nudge</a> functions (<code><span class='Function'>»«</span></code>), <a href="couple.html">Merge</a> (<code><span class='Function'>></span></code>), and <a href="reshape.html">Reshape</a> (<code><span class='Function'>⥊</span></code>).</p> <p>Arrays are value types (or immutable), so that there is no way to "change" the shape or elements of an array. An array with different properties is a different array. As a consequence, arrays are an inductive type, and it's not possible for an array to contain itself, or contain an array that contains itself, and so on. However, it is possible for an array to contain a function or other operation that has access to the array through a variable, and in this sense an array can "know about" itself.</p> <p>Different elements of an array should not influence each other. While some APLs force numbers placed in the same array to a common representation, which may have different precision properties, BQN values must not change behavior when placed in an array. However, this doesn't preclude changing the storage type of an array for better performance: for example, in a BQN implementation using 64-bit floats, an array whose elements are all integers that fit in 32-bit int range might be represented as an array of 32-bit ints.</p> <h2 id="operation-types"><a class="header" href="#operation-types">Operation types</a></h2> -<p><em>Full documentation <a href="ops.html">here</a>.</em></p> +<p><em><a href="ops.html">Full documentation</a>.</em></p> <p>An operation is either a function or modifier, and can be applied to <em>inputs</em>—which are called <em>arguments</em> for functions and <em>operands</em> for modifiers—to obtain a result. During this application an operation might also change variables within its scope and call other operations, or cause an error, in which case it doesn't return a result. There is one type of call for each of the three operation types, and an operation will give an error if it is called in a way that doesn't match its type.</p> <p>In BQN syntax the result of a function has a subject role and the result of a modifier has a function role. However, the result can be any value at all: roles take place at the syntactic level, which has no bearing on types and values in the semantic level. This distinction is discussed further in <a href="context.html#mixing-roles">Mixing roles</a>.</p> <h3 id="functions"><a class="header" href="#functions">Functions</a></h3> @@ -71,5 +71,5 @@ <h3 id="modifiers"><a class="header" href="#modifiers">Modifiers</a></h3> <p>A 1-modifier is called with one operand, while a 2-modifier is called with two. In contrast to functions, these are distinct types, and it is impossible to have a value that can be called with either one or two operands. Also in contrast to functions, data values cannot be called as modifiers: they will cause an error if called this way.</p> <h2 id="namespaces"><a class="header" href="#namespaces">Namespaces</a></h2> -<p><em>Full documentation <a href="namespace.html">here</a>.</em></p> +<p><em><a href="namespace.html">Full documentation</a>.</em></p> <p>Functions and modifiers have internal scopes which they can manipulate (by defining and modifying variables) to save and update information. Namespaces let the programmer to expose this state more directly: identifiers in a namespace may be exported, allowing code outside the namespace to read their values.</p> |
