diff options
Diffstat (limited to 'docs/tutorial')
| -rw-r--r-- | docs/tutorial/expression.html | 38 | ||||
| -rw-r--r-- | docs/tutorial/list.html | 50 |
2 files changed, 44 insertions, 44 deletions
diff --git a/docs/tutorial/expression.html b/docs/tutorial/expression.html index bb5fc9d6..cea81292 100644 --- a/docs/tutorial/expression.html +++ b/docs/tutorial/expression.html @@ -8,7 +8,7 @@ <h2 id="arithmetic">Arithmetic</h2> <p>All right, let's get started! Since you can run BQN online from the <a href="https://mlochbaum.github.io/BQN/try.html">REPL</a> there aren't any real technical preliminaries, but if you'd like to look at non-web-based options head over to <a href="../running.html">running.md</a>.</p> <p>In the code blocks shown here, input is highlighted and indented, while output is not colored or indented. To experiment with the code, you can click the <code><span class='Value'>↗️</span></code> arrow in the top right corner to open it in the REPL.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiArIDMKNi0gICA1Ci0gMS41&run">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>+</span> <span class='Number'>3</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiArIDMKNi0gICA1Ci0gMS41">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>+</span> <span class='Number'>3</span> 5 <span class='Number'>6</span><span class='Function'>-</span> <span class='Number'>5</span> 1 @@ -32,7 +32,7 @@ <p>Shown above are a few arithmetic operations. BQN manages to pass as a normal programming language for three lines so far. That's a big accomplishment for BQN! Earth's a confusing place!</p> <p>The number of spaces between <em>primitive functions</em> like <code><span class='Function'>+</span></code> and <code><span class='Function'>-</span></code> and their <em>arguments</em> doesn't matter: you can use as much or as little as you like. No spaces inside numbers, of course.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiDDlyDPgAo5IMO3IDIKw7cg4oie&run">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>×</span> <span class='Number'>π</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiDDlyDPgAo5IMO3IDIKw7cg4oie">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>×</span> <span class='Number'>π</span> 6.28318530717959 <span class='Number'>9</span> <span class='Function'>÷</span> <span class='Number'>2</span> 4.5 @@ -66,7 +66,7 @@ <p>Okay, now BQN looks like normal (grade-school) mathematics, which is sort of like looking normal. Pi (<code><span class='Number'>π</span></code>) represents <a href="https://en.wikipedia.org/wiki/Pi">that real famous number</a> and Infinity (<code><span class='Number'>∞</span></code>) is also part of the number system (the BQN spec allows an implementation to choose its number system, and all existing implementations use double-precision floats, like Javascript or Lua). In analogy with the one-argument form of Minus (<code><span class='Function'>-</span></code>) giving the negation of a number, Divide (<code><span class='Function'>÷</span></code>) with only one argument gives its reciprocal.</p> <p>A number can be raised to the power of another with Power, written <code><span class='Function'>⋆</span></code>. That's a star rather than an asterisk; BQN doesn't use the asterisk symbol. If it's called without a left argument, then <code><span class='Function'>⋆</span></code> uses a base of <a href="https://en.wikipedia.org/wiki/E_(mathematical_constant)">Euler's number</a> <em>e</em> and is called Exponential.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiDii4YgMwozIOKLhiAyCuKLhiAxICAgIyBUaGVyZSdzIG5vIGNvbnN0YW50IGZvciBlIGJ1dCB5b3UgY2FuIGdldCBpdCB0aGlzIHdheQrii4YgMi4z&run">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>⋆</span> <span class='Number'>3</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiDii4YgMwozIOKLhiAyCuKLhiAxICAgIyBUaGVyZSdzIG5vIGNvbnN0YW50IGZvciBlIGJ1dCB5b3UgY2FuIGdldCBpdCB0aGlzIHdheQrii4YgMi4z">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>⋆</span> <span class='Number'>3</span> 8 <span class='Number'>3</span> <span class='Function'>⋆</span> <span class='Number'>2</span> 9 @@ -91,7 +91,7 @@ </table> <p>You could use Power to take square roots and <em>n</em>-th roots, but BQN also provides the primitive <code><span class='Function'>√</span></code> for this purpose. If no left argument is provided, then it is the Square Root function; with a left argument it is called Root and raises the right argument to the power of one divided by the left argument.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oiaIDIKMyDiiJogMjc=&run">↗️</a><pre> <span class='Function'>√</span> <span class='Number'>2</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oiaIDIKMyDiiJogMjc=">↗️</a><pre> <span class='Function'>√</span> <span class='Number'>2</span> 1.4142135623731 <span class='Number'>3</span> <span class='Function'>√</span> <span class='Number'>27</span> 3 @@ -104,14 +104,14 @@ <p>In case you're wondering, Logarithm—the other inverse of Power—is written <code><span class='Function'>⋆</span><span class='Modifier'>⁼</span></code>. We'll get to it in the section on modifiers.</p> <h2 id="compound-expressions">Compound expressions</h2> <p>It's sometimes useful to write programs with more than one function in them. Here is where BQN and any sort of normality part ways.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MsOXMyAtIDUKKDLDlzMpIC0gNQ==&run">↗️</a><pre> <span class='Number'>2</span><span class='Function'>×</span><span class='Number'>3</span> <span class='Function'>-</span> <span class='Number'>5</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MsOXMyAtIDUKKDLDlzMpIC0gNQ==">↗️</a><pre> <span class='Number'>2</span><span class='Function'>×</span><span class='Number'>3</span> <span class='Function'>-</span> <span class='Number'>5</span> ¯4 <span class='Paren'>(</span><span class='Number'>2</span><span class='Function'>×</span><span class='Number'>3</span><span class='Paren'>)</span> <span class='Function'>-</span> <span class='Number'>5</span> 1 </pre> <p>I bet if you try hard you'll remember how much you hated learning to do exponentiation before multiplication and division before addition and subtraction. Didn't I tell you Earth was a confusing place? BQN treats all functions—not just primitives but the ones you'll define as well—the same way. They are evaluated from right to left, and parentheses can be used to group subexpressions that have to be evaluated before being used as arguments.</p> <p>For a longer example, here's an expression for the <a href="https://en.wikipedia.org/wiki/Sphere#Enclosed_volume">volume of a sphere</a> with radius 2.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KDTDtzMpIMOXIM+AIMOXIDLii4Yz&run">↗️</a><pre> <span class='Paren'>(</span><span class='Number'>4</span><span class='Function'>÷</span><span class='Number'>3</span><span class='Paren'>)</span> <span class='Function'>×</span> <span class='Number'>π</span> <span class='Function'>×</span> <span class='Number'>2</span><span class='Function'>⋆</span><span class='Number'>3</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KDTDtzMpIMOXIM+AIMOXIDLii4Yz">↗️</a><pre> <span class='Paren'>(</span><span class='Number'>4</span><span class='Function'>÷</span><span class='Number'>3</span><span class='Paren'>)</span> <span class='Function'>×</span> <span class='Number'>π</span> <span class='Function'>×</span> <span class='Number'>2</span><span class='Function'>⋆</span><span class='Number'>3</span> 33.5103216382911 </pre> <p>The evaluation order is shown below, with the function <code><span class='Function'>⋆</span></code> on the first line evaluated first, then <code><span class='Function'>×</span></code> on the next, and so on. The effect of the parentheses is that <code><span class='Function'>÷</span></code> is evaluated before the leftmost <code><span class='Function'>×</span></code>.</p> @@ -158,13 +158,13 @@ <p>The following rule might help you to internalize this system in addition to identifying when parentheses are needed: an expression never needs to end with a parenthesis, or contain two closing parentheses in a row. If it does, at least one set of parentheses can be removed.</p> <h2 id="one-or-two-arguments">One or two arguments?</h2> <p>What about functions without a left argument? Let's find an equation with lots of square roots in it… <a href="https://en.wikipedia.org/wiki/Nested_radical#Denesting">looks good</a>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oiaIDMgKyAyIMOXIOKImjIKMSArIOKImjI=&run">↗️</a><pre> <span class='Function'>√</span> <span class='Number'>3</span> <span class='Function'>+</span> <span class='Number'>2</span> <span class='Function'>×</span> <span class='Function'>√</span><span class='Number'>2</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oiaIDMgKyAyIMOXIOKImjIKMSArIOKImjI=">↗️</a><pre> <span class='Function'>√</span> <span class='Number'>3</span> <span class='Function'>+</span> <span class='Number'>2</span> <span class='Function'>×</span> <span class='Function'>√</span><span class='Number'>2</span> 2.4142135623731 <span class='Number'>1</span> <span class='Function'>+</span> <span class='Function'>√</span><span class='Number'>2</span> 2.4142135623731 </pre> <p>They are the same, and now you can't say that BQN is the most complicated thing on this particular page! Just to make sure, we can find the difference by subtracting them, but we need to put the left argument in parentheses:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KOKImjMgKyAyw5fiiJoyKSAtIDEr4oiaMg==&run">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>√</span><span class='Number'>3</span> <span class='Function'>+</span> <span class='Number'>2</span><span class='Function'>×√</span><span class='Number'>2</span><span class='Paren'>)</span> <span class='Function'>-</span> <span class='Number'>1</span><span class='Function'>+√</span><span class='Number'>2</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KOKImjMgKyAyw5fiiJoyKSAtIDEr4oiaMg==">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>√</span><span class='Number'>3</span> <span class='Function'>+</span> <span class='Number'>2</span><span class='Function'>×√</span><span class='Number'>2</span><span class='Paren'>)</span> <span class='Function'>-</span> <span class='Number'>1</span><span class='Function'>+√</span><span class='Number'>2</span> 0 </pre> <p>That's a fairly large expression, so here's another evaluation diagram to check your understanding.</p> @@ -228,11 +228,11 @@ <p>Perhaps more than you thought! To really get roles, it's important to understand that a role is properly a property of an expression, and not its value. In language implementation terms, roles are used only to parse expressions, giving a syntax tree, but don't dictate what values are possible when the tree is evaluated. So it's possible to have a function with a number role or a number with a function role. The reason this doesn't happen with the numeric literals and primitives we've introduced is that these tokens have a constant value. <code><span class='Function'>×</span></code> or <code><span class='Number'>∞</span></code> have the same value in any possible program, and so it makes sense that their types and roles should correspond. When we introduce identifiers, we'll see this correspondence break down—and why that's good!</p> <h2 id="character-arithmetic">Character arithmetic</h2> <p>Gosh, that's a lot of arithmetic up there. Maybe adding characters will mix things up a bit? Hang on, you can't add characters, only subtract them… let's back up.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=J2Mn&run">↗️</a><pre> <span class='String'>'c'</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=J2Mn">↗️</a><pre> <span class='String'>'c'</span> 'c' </pre> <p>A character is written with single quotes. As in the C language, it's not the same as a string, which is written with double quotes. There are no escapes for characters: any source code character between single quotes becomes a character literal, even a newline. Characters permit some kinds of arithmetic:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=J2MnICsgMQonaCcgLSAnYSc=&run">↗️</a><pre> <span class='String'>'c'</span> <span class='Function'>+</span> <span class='Number'>1</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=J2MnICsgMQonaCcgLSAnYSc=">↗️</a><pre> <span class='String'>'c'</span> <span class='Function'>+</span> <span class='Number'>1</span> 'd' <span class='String'>'h'</span> <span class='Function'>-</span> <span class='String'>'a'</span> 7 @@ -250,15 +250,15 @@ <li>Pointers in a computer's memory. I'll let you verify for yourself that the rules are the same.</li> </ul> <p>Want to shift an uppercase character to lowercase? Affine characters to the rescue:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=J0snICsgJ2EnLSdBJw==&run">↗️</a><pre> <span class='String'>'K'</span> <span class='Function'>+</span> <span class='String'>'a'</span><span class='Function'>-</span><span class='String'>'A'</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=J0snICsgJ2EnLSdBJw==">↗️</a><pre> <span class='String'>'K'</span> <span class='Function'>+</span> <span class='String'>'a'</span><span class='Function'>-</span><span class='String'>'A'</span> 'k' </pre> <p>Convert a character to a digit? Here you go:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=JzQnIC0gJzAn&run">↗️</a><pre> <span class='String'>'4'</span> <span class='Function'>-</span> <span class='String'>'0'</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=JzQnIC0gJzAn">↗️</a><pre> <span class='String'>'4'</span> <span class='Function'>-</span> <span class='String'>'0'</span> 4 </pre> <p>The one thing affine characters won't let you do is find some special "starting character": the main distinguishing factor between a linear and an affine space is that an affine space has no origin. However, for some kinds of programming finding a character's code point is important, so BQN also includes a special literal for the null character, written <code><span class='String'>@</span></code>:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=JyonIC0gQApAICsgOTc=&run">↗️</a><pre> <span class='String'>'*'</span> <span class='Function'>-</span> <span class='String'>@</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=JyonIC0gQApAICsgOTc=">↗️</a><pre> <span class='String'>'*'</span> <span class='Function'>-</span> <span class='String'>@</span> 42 <span class='String'>@</span> <span class='Function'>+</span> <span class='Number'>97</span> 'a' @@ -280,13 +280,13 @@ <p>Addition and subtraction with affine characters have all the same algebraic properties that they do with numbers. One way to see this is to think of values as a combination of "characterness" (0 for numbers and 1 for characters) and either numeric value or code point. Addition and subtraction are done element-wise on these pairs of numbers, and are allowed if the result is a valid value, that is, its characterness is 0 or 1 and its value is a valid code point if the characterness is 1. However, because the space of values is no longer closed under addition and subtraction, certain rearrangements of valid computations might not be valid, if one of the values produced in the middle isn't legal.</p> <h2 id="modifiers">Modifiers</h2> <p>Functions are nice and all, but to really bring us into the space age BQN has a second level of function called <em>modifiers</em> (the space age in this case is when operators were introduced to APL in the early 60s—hey, did you know the <a href="https://aplwiki.com/wiki/APL_conference#1970">second APL conference</a> was held at Goddard Space Flight Center?). While functions apply to subjects, modifiers can apply to functions <em>or</em> subjects, and return functions. For example, the 1-modifier <code><span class='Modifier'>˜</span></code> modifies one function by swapping the arguments before calling it (Swap), or copying the right argument to the left if there's only one (Self).</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiAty5wgJ2QnICAjIFN1YnRyYWN0IGZyb20KK8ucIDMgICAgICAjIEFkZCB0byBpdHNlbGY=&run">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>'d'</span> <span class='Comment'># Subtract from +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiAty5wgJ2QnICAjIFN1YnRyYWN0IGZyb20KK8ucIDMgICAgICAjIEFkZCB0byBpdHNlbGY=">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>'d'</span> <span class='Comment'># Subtract from </span>'b' <span class='Function'>+</span><span class='Modifier'>˜</span> <span class='Number'>3</span> <span class='Comment'># Add to itself </span>6 </pre> <p>This gives us two nice ways to square a value:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=w5fLnCA0CjIg4ouGy5wgNA==&run">↗️</a><pre> <span class='Function'>×</span><span class='Modifier'>˜</span> <span class='Number'>4</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=w5fLnCA0CjIg4ouGy5wgNA==">↗️</a><pre> <span class='Function'>×</span><span class='Modifier'>˜</span> <span class='Number'>4</span> 16 <span class='Number'>2</span> <span class='Function'>⋆</span><span class='Modifier'>˜</span> <span class='Number'>4</span> 16 @@ -312,11 +312,11 @@ </table> <p>Another 1-modifier is Undo (<code><span class='Modifier'>⁼</span></code>). BQN has just enough computer algebra facilities to look like a tool for Neanderthals next to a real computer algebra system, and among them is the ability to invert some primitives. In general you can't be sure when Undo will work (it might even be undecidable), but the examples I'll give here are guaranteed by <a href="../spec/inferred.html#undo">the spec</a> to always work in the same way. Starting with a <em>third</em> way to square a number:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oia4oG8IDQ=&run">↗️</a><pre> <span class='Function'>√</span><span class='Modifier'>⁼</span> <span class='Number'>4</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oia4oG8IDQ=">↗️</a><pre> <span class='Function'>√</span><span class='Modifier'>⁼</span> <span class='Number'>4</span> 16 </pre> <p>The most important use for Undo in arithmetic is the logarithm, written <code><span class='Function'>⋆</span><span class='Modifier'>⁼</span></code>. That's all a logarithm is: it undoes the Power function! With no left argument <code><span class='Function'>⋆</span><span class='Modifier'>⁼</span></code> is the natural logarithm. If there's a left argument then Undo considers it part of the function to be undone. The result in this case is that <code><span class='Function'>⋆</span><span class='Modifier'>⁼</span></code> with two arguments is the logarithm of the right argument with base given by the left one.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4ouG4oG8IDEwCjIg4ouG4oG8IDMyICAgICMgTG9nIGJhc2UgMgoyIOKLhiAyIOKLhuKBvCAzMgoxMCDii4bigbwgMWU0ICAjIExvZyBiYXNlIDEwIG9mIGEgbnVtYmVyIGluIHNjaWVudGlmaWMgbm90YXRpb24=&run">↗️</a><pre> <span class='Function'>⋆</span><span class='Modifier'>⁼</span> <span class='Number'>10</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4ouG4oG8IDEwCjIg4ouG4oG8IDMyICAgICMgTG9nIGJhc2UgMgoyIOKLhiAyIOKLhuKBvCAzMgoxMCDii4bigbwgMWU0ICAjIExvZyBiYXNlIDEwIG9mIGEgbnVtYmVyIGluIHNjaWVudGlmaWMgbm90YXRpb24=">↗️</a><pre> <span class='Function'>⋆</span><span class='Modifier'>⁼</span> <span class='Number'>10</span> 2.30258509299405 <span class='Number'>2</span> <span class='Function'>⋆</span><span class='Modifier'>⁼</span> <span class='Number'>32</span> <span class='Comment'># Log base 2 </span>5 @@ -326,14 +326,14 @@ </span>4 </pre> <p>Another 1-modifier, which at the moment is tremendously useless, is Constant <code><span class='Modifier'>˙</span></code>. It turns its operand into a constant function that always returns the operand (inputs to modifiers are called <em>operands</em> because <em>modificands</em> is just too horrible).</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiAzy5kgNA==&run">↗️</a><pre> <span class='Number'>2</span> <span class='Number'>3</span><span class='Modifier'>˙</span> <span class='Number'>4</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiAzy5kgNA==">↗️</a><pre> <span class='Number'>2</span> <span class='Number'>3</span><span class='Modifier'>˙</span> <span class='Number'>4</span> 3 </pre> <p>Well, I guess it's not pedagogically useless, as it does demonstrate that a modifier can be applied to subjects as well as functions. Even though <code><span class='Number'>3</span></code> is a subject, <code><span class='Number'>3</span><span class='Modifier'>˙</span></code> is a function, and can be applied to and ignore the two arguments <code><span class='Number'>2</span></code> and <code><span class='Number'>4</span></code>.</p> <p>With three examples you may have noticed that 1-modifiers tend to cluster at the top of the screen. In fact, every primitive 1-modifer is a superscript character: we've covered <code><span class='Modifier'>˜⁼˙</span></code>, and the remaining array-based modifiers <code><span class='Modifier'>˘¨⌜´˝`</span></code> will show up later.</p> <h2 id="2-modifiers">2-modifiers</h2> <p>Made it to the last role, the 2-modifier (if you think something's been skipped, you're free to call subjects 0-modifiers. They don't modify anything. Just not when other people can hear you). To introduce them we'll use Atop <code><span class='Modifier2'>∘</span></code>, which works a lot like mathematical composition except that it uses one or two arguments. These arguments are passed to the function on the right, and the result is passed to the function on the left. So the function on the left is only ever called with one argument.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyDDl8uc4oiYKyA0ICAjIFNxdWFyZSBvZiAzIHBsdXMgNAot4oiYKMOXy5wpIDUgICMgTmVnYXRpdmUgc3F1YXJlIG9mIDU=&run">↗️</a><pre> <span class='Number'>3</span> <span class='Function'>×</span><span class='Modifier'>˜</span><span class='Modifier2'>∘</span><span class='Function'>+</span> <span class='Number'>4</span> <span class='Comment'># Square of 3 plus 4 +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyDDl8uc4oiYKyA0ICAjIFNxdWFyZSBvZiAzIHBsdXMgNAot4oiYKMOXy5wpIDUgICMgTmVnYXRpdmUgc3F1YXJlIG9mIDU=">↗️</a><pre> <span class='Number'>3</span> <span class='Function'>×</span><span class='Modifier'>˜</span><span class='Modifier2'>∘</span><span class='Function'>+</span> <span class='Number'>4</span> <span class='Comment'># Square of 3 plus 4 </span>49 <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'>5</span> <span class='Comment'># Negative square of 5 </span>¯25 diff --git a/docs/tutorial/list.html b/docs/tutorial/list.html index deae0f11..71ad8237 100644 --- a/docs/tutorial/list.html +++ b/docs/tutorial/list.html @@ -6,11 +6,11 @@ <div class="nav"><a href="https://github.com/mlochbaum/BQN">BQN</a> / <a href="../index.html">main</a> / <a href="index.html">tutorial</a></div> <h1 id="tutorial-working-with-lists">Tutorial: Working with lists</h1> <p>Enough with all these preliminaries like learning how to read basic expressions. Let's get into what makes BQN special.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oMSwgMiwgM+KfqQ==&run">↗️</a><pre> <span class='Bracket'>⟨</span><span class='Number'>1</span><span class='Separator'>,</span> <span class='Number'>2</span><span class='Separator'>,</span> <span class='Number'>3</span><span class='Bracket'>⟩</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oMSwgMiwgM+KfqQ==">↗️</a><pre> <span class='Bracket'>⟨</span><span class='Number'>1</span><span class='Separator'>,</span> <span class='Number'>2</span><span class='Separator'>,</span> <span class='Number'>3</span><span class='Bracket'>⟩</span> ⟨ 1 2 3 ⟩ </pre> <p>This is a list. Wait for it…</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oMSwgMiwgM+KfqSArIDE=&run">↗️</a><pre> <span class='Bracket'>⟨</span><span class='Number'>1</span><span class='Separator'>,</span> <span class='Number'>2</span><span class='Separator'>,</span> <span class='Number'>3</span><span class='Bracket'>⟩</span> <span class='Function'>+</span> <span class='Number'>1</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oMSwgMiwgM+KfqSArIDE=">↗️</a><pre> <span class='Bracket'>⟨</span><span class='Number'>1</span><span class='Separator'>,</span> <span class='Number'>2</span><span class='Separator'>,</span> <span class='Number'>3</span><span class='Bracket'>⟩</span> <span class='Function'>+</span> <span class='Number'>1</span> ⟨ 2 3 4 ⟩ </pre> <p>There we go. Now in BQN arrays are not just lists, which are a 1-dimensional data structure, but can have any number of dimensions. In this tutorial we're going to discuss lists only, leaving the 5-dimensional stuff for later. So we're really only seeing the power of <a href="https://aplwiki.com/wiki/K">K</a>, an APL-family language that only uses lists (and dictionaries, which BQN doesn't have). K was powerful enough for Arthur Whitney to found <a href="https://en.wikipedia.org/wiki/Kx_Systems">two</a> <a href="https://shakti.com/">companies</a> and make millions and millions of dollars, and BQN's compiler also runs almost entirely on lists, so this is probably enough power for one webpage.</p> @@ -79,7 +79,7 @@ </pre> <p>Strand notation is shorter and looks less cluttered in this example. As with lists, anything goes in a strand, but if it's the result of a function or operator, or another strand, then it has to be put in parentheses. With one set of parentheses, a strand will be just as long as the equivalent bracketed list, and with two you're better off using the list.</p> <p>A ligature is a kind of notation and doesn't do something specific like a function does. It's the sequence of ligatures that makes whatever they join together into a list. So if we parenthesize either ligature below, we get a different result! Ligatures aren't right-associative or left-associative.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MOKAvzHigL8yCigw4oC/MSnigL8yCjDigL8oMeKAvzIp&run">↗️</a><pre> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MOKAvzHigL8yCigw4oC/MSnigL8yCjDigL8oMeKAvzIp">↗️</a><pre> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span> ⟨ 0 1 2 ⟩ <span class='Paren'>(</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Paren'>)</span><span class='Ligature'>‿</span><span class='Number'>2</span> ⟨ ⟨ 0 1 ⟩ 2 ⟩ @@ -113,7 +113,7 @@ <p>Lists are just one-dimensional arrays. Types are divided into <em>data types</em>, which tend to have a subject role, and <em>operation types</em>, which tend to have a role matching their type. Also, any value that's not an array, such as everything we used in the last tutorial, is called an <em>atom</em>.</p> <h2 id="arithmetic-on-lists">Arithmetic on lists</h2> <p>Arithmetic functions automatically apply to each element of a list argument. If both arguments are lists, they have to have the same length, and they're matched up one element at a time.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=w7cg4p+oMiwzLDTin6kKCiJBUEwiICsgMQoKIjMxNDE1IiAtICcwJwoKNOKAvzPigL8y4oC/MSDii4YgMeKAvzLigL8z4oC/NA==&run">↗️</a><pre> <span class='Function'>÷</span> <span class='Bracket'>⟨</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>3</span><span class='Separator'>,</span><span class='Number'>4</span><span class='Bracket'>⟩</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=w7cg4p+oMiwzLDTin6kKCiJBUEwiICsgMQoKIjMxNDE1IiAtICcwJwoKNOKAvzPigL8y4oC/MSDii4YgMeKAvzLigL8z4oC/NA==">↗️</a><pre> <span class='Function'>÷</span> <span class='Bracket'>⟨</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>3</span><span class='Separator'>,</span><span class='Number'>4</span><span class='Bracket'>⟩</span> ⟨ 0.5 0.333333333333333 0.25 ⟩ <span class='String'>"APL"</span> <span class='Function'>+</span> <span class='Number'>1</span> @@ -126,7 +126,7 @@ ⟨ 4 9 8 1 ⟩ </pre> <p>This list application works recursively, so that lists of lists (and so on) are handled as well. We say that arithmetic functions are <em>pervasive</em>. They dig into their arguments until reaching the atoms.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiDDlyDin6gw4oC/MiDii4QgMeKAvzPigL814p+pCgrin6ggMTAsIDIw4oC/MzAg4p+pICsg4p+oIDHigL8yLCAzIOKfqQ==&run">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>×</span> <span class='Bracket'>⟨</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>2</span> <span class='Separator'>⋄</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>5</span><span class='Bracket'>⟩</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiDDlyDin6gw4oC/MiDii4QgMeKAvzPigL814p+pCgrin6ggMTAsIDIw4oC/MzAg4p+pICsg4p+oIDHigL8yLCAzIOKfqQ==">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>×</span> <span class='Bracket'>⟨</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>2</span> <span class='Separator'>⋄</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>5</span><span class='Bracket'>⟩</span> ⟨ ⟨ 0 4 ⟩ ⟨ 2 6 10 ⟩ ⟩ <span class='Bracket'>⟨</span> <span class='Number'>10</span><span class='Separator'>,</span> <span class='Number'>20</span><span class='Ligature'>‿</span><span class='Number'>30</span> <span class='Bracket'>⟩</span> <span class='Function'>+</span> <span class='Bracket'>⟨</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Separator'>,</span> <span class='Number'>3</span> <span class='Bracket'>⟩</span> @@ -156,14 +156,14 @@ <p>Let's introduce a few primitives to work with lists.</p> <p>Make one or two atom arguments into a list with <code><span class='Function'>≍</span></code>, pronounced Solo in the one-argument case and Couple in the two-argument case. This might not seem to merit a symbol but there's more to come. Don't call it on lists and ponder the results, igniting a hunger for ever more dimensions.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omNIDQKCjIg4omNIDQ=&run">↗️</a><pre> <span class='Function'>≍</span> <span class='Number'>4</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omNIDQKCjIg4omNIDQ=">↗️</a><pre> <span class='Function'>≍</span> <span class='Number'>4</span> ⟨ 4 ⟩ <span class='Number'>2</span> <span class='Function'>≍</span> <span class='Number'>4</span> ⟨ 2 4 ⟩ </pre> <p>Concatenate lists with Join To (<code><span class='Function'>∾</span></code>). The little chain link symbol—technically "inverted lazy S"—is my favorite in BQN. Hook those lists together!</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oMSwyLDPin6kg4oi+ICJhYmMiCgowIOKIviDin6gxLDIsM+KfqQoKInBsdXJhbCIg4oi+ICdzJw==&run">↗️</a><pre> <span class='Bracket'>⟨</span><span class='Number'>1</span><span class='Separator'>,</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>3</span><span class='Bracket'>⟩</span> <span class='Function'>∾</span> <span class='String'>"abc"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oMSwyLDPin6kg4oi+ICJhYmMiCgowIOKIviDin6gxLDIsM+KfqQoKInBsdXJhbCIg4oi+ICdzJw==">↗️</a><pre> <span class='Bracket'>⟨</span><span class='Number'>1</span><span class='Separator'>,</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>3</span><span class='Bracket'>⟩</span> <span class='Function'>∾</span> <span class='String'>"abc"</span> ⟨ 1 2 3 'a' 'b' 'c' ⟩ <span class='Number'>0</span> <span class='Function'>∾</span> <span class='Bracket'>⟨</span><span class='Number'>1</span><span class='Separator'>,</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>3</span><span class='Bracket'>⟩</span> @@ -174,11 +174,11 @@ </pre> <p>The last two examples show that you can join a list to an atom, making it the first or last element of the result. This is a little suspect because if you decide the data being stored is more complicated and start using a list instead of an atom, then it will no longer be used as a single element but rather a subsection of the result. So I would only use that shortcut for something like a numeric literal that's clearly an atom and will stay that way, and otherwise wrap those atomic arguments in some <code><span class='Bracket'>⟨⟩</span></code> brackets. Join will even work with two atoms, but in that case I'd say it makes more sense to use Couple instead.</p> <p>Reverse (<code><span class='Function'>⌽</span></code>) puts the list back to front.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oy9ICJkcmF3ZXIi&run">↗️</a><pre> <span class='Function'>⌽</span> <span class='String'>"drawer"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oy9ICJkcmF3ZXIi">↗️</a><pre> <span class='Function'>⌽</span> <span class='String'>"drawer"</span> "reward" </pre> <p>With a left argument <code><span class='Function'>⌽</span></code> means Rotate instead, and shifts values over by the specified amount, wrapping those that go off the end to the other side. A positive value rotates to the left, and a negative one rotates right.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiDijL0g4p+oMCwxLDIsMyw04p+pCsKvMSDijL0gImJjZGVhIg==&run">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>⌽</span> <span class='Bracket'>⟨</span><span class='Number'>0</span><span class='Separator'>,</span><span class='Number'>1</span><span class='Separator'>,</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>3</span><span class='Separator'>,</span><span class='Number'>4</span><span class='Bracket'>⟩</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiDijL0g4p+oMCwxLDIsMyw04p+pCsKvMSDijL0gImJjZGVhIg==">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>⌽</span> <span class='Bracket'>⟨</span><span class='Number'>0</span><span class='Separator'>,</span><span class='Number'>1</span><span class='Separator'>,</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>3</span><span class='Separator'>,</span><span class='Number'>4</span><span class='Bracket'>⟩</span> ⟨ 2 3 4 0 1 ⟩ <span class='Number'>¯1</span> <span class='Function'>⌽</span> <span class='String'>"bcdea"</span> "abcde" @@ -204,7 +204,7 @@ </table> <p>The 1-modifier Each (<code><span class='Modifier'>¨</span></code>) applies its operand to every element of a list argument: it's the same as <code><span class='Value'>map</span></code> in a functional programming language. With two list arguments (which have to have the same length), Each pairs the corresponding elements from each, a bit like a <code><span class='Value'>zip</span></code> function. If one argument is a list and one's an atom, the atom is reused every time instead.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oy9wqggImFiY2Qi4oC/IkFCQ0RFRiLigL8iMDEiCgoic3RyaW5nIuKAvyJsaXN0IuKAvyJhcnJheSIg4oi+wqggJ3MnCgoiYWJjIiDiiY3CqCDijL0gImFiYyI=&run">↗️</a><pre> <span class='Function'>⌽</span><span class='Modifier'>¨</span> <span class='String'>"abcd"</span><span class='Ligature'>‿</span><span class='String'>"ABCDEF"</span><span class='Ligature'>‿</span><span class='String'>"01"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oy9wqggImFiY2Qi4oC/IkFCQ0RFRiLigL8iMDEiCgoic3RyaW5nIuKAvyJsaXN0IuKAvyJhcnJheSIg4oi+wqggJ3MnCgoiYWJjIiDiiY3CqCDijL0gImFiYyI=">↗️</a><pre> <span class='Function'>⌽</span><span class='Modifier'>¨</span> <span class='String'>"abcd"</span><span class='Ligature'>‿</span><span class='String'>"ABCDEF"</span><span class='Ligature'>‿</span><span class='String'>"01"</span> ⟨ "dcba" "FEDCBA" "10" ⟩ <span class='String'>"string"</span><span class='Ligature'>‿</span><span class='String'>"list"</span><span class='Ligature'>‿</span><span class='String'>"array"</span> <span class='Function'>∾</span><span class='Modifier'>¨</span> <span class='String'>'s'</span> @@ -214,13 +214,13 @@ ⟨ "ac" "bb" "ca" ⟩ </pre> <p>Fold (<code><span class='Modifier'>´</span></code>) is the higher-order function also known as reduce or accumulate. It applies its operand function between each pair of elements in a list argument. For example, <code><span class='Function'>+</span><span class='Modifier'>´</span></code> gives the sum of a list and <code><span class='Function'>×</span><span class='Modifier'>´</span></code> gives its product.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8K0IDLigL8z4oC/NArDl8K0IDLigL8z4oC/NA==&run">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>´</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>4</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8K0IDLigL8z4oC/NArDl8K0IDLigL8z4oC/NA==">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>´</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>4</span> 9 <span class='Function'>×</span><span class='Modifier'>´</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>4</span> 24 </pre> <p>To match the order of BQN evaluation, Fold moves over its argument array from right to left. You'd get the same result by writing the operand function in between each element of the argument list, but you'd also write the function a lot of times.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LcK0IDHigL8y4oC/M+KAvzTigL81CjEtMi0zLTQtNQ==&run">↗️</a><pre> <span class='Function'>-</span><span class='Modifier'>´</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='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>5</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LcK0IDHigL8y4oC/M+KAvzTigL81CjEtMi0zLTQtNQ==">↗️</a><pre> <span class='Function'>-</span><span class='Modifier'>´</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='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>5</span> 3 <span class='Number'>1</span><span class='Function'>-</span><span class='Number'>2</span><span class='Function'>-</span><span class='Number'>3</span><span class='Function'>-</span><span class='Number'>4</span><span class='Function'>-</span><span class='Number'>5</span> 3 @@ -228,11 +228,11 @@ <p>With this evaluation order, <code><span class='Function'>-</span><span class='Modifier'>´</span></code> gives the <em>alternating sum</em> of its argument. Think of it this way: the left argument of each <code><span class='Function'>-</span></code> is a single number, while the right argument is made up of all the numbers to the right subtracted together. So each <code><span class='Function'>-</span></code> flips the sign of every number to its right, and every number is negated by all the <code><span class='Function'>-</span></code>s to its left. The first number (<code><span class='Number'>1</span></code> above) never gets negated, the second is negated once, the third is negated twice, returning it to its original value… the signs alternate.</p> <p><em>Hey, isn't it dissonant that the first, second, and third numbers are negated zero, one, and two times? If they were the zeroth, first, and second it wouldn't be…</em></p> <p>You can fold with the Join To function to join several lists together:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oi+wrQg4p+oICJjb24iLCAiY2F0IiwgImVuYXQiLCAiZSIg4p+p&run">↗️</a><pre> <span class='Function'>∾</span><span class='Modifier'>´</span> <span class='Bracket'>⟨</span> <span class='String'>"con"</span><span class='Separator'>,</span> <span class='String'>"cat"</span><span class='Separator'>,</span> <span class='String'>"enat"</span><span class='Separator'>,</span> <span class='String'>"e"</span> <span class='Bracket'>⟩</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oi+wrQg4p+oICJjb24iLCAiY2F0IiwgImVuYXQiLCAiZSIg4p+p">↗️</a><pre> <span class='Function'>∾</span><span class='Modifier'>´</span> <span class='Bracket'>⟨</span> <span class='String'>"con"</span><span class='Separator'>,</span> <span class='String'>"cat"</span><span class='Separator'>,</span> <span class='String'>"enat"</span><span class='Separator'>,</span> <span class='String'>"e"</span> <span class='Bracket'>⟩</span> "concatenate" </pre> <p>But you shouldn't! Just <code><span class='Function'>∾</span></code> will do the job for you—with no left argument it's just called "Join" (it's like Javascript's <code><span class='Number'>.join</span><span class='Paren'>()</span></code>, but with no separator and not specific to strings). And it could do more jobs if you had more dimensions. But I'm sure that's the furthest thing from your mind.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oi+IOKfqCAiY29uIiwgImNhdCIsICJlbmF0IiwgImUiIOKfqQ==&run">↗️</a><pre> <span class='Function'>∾</span> <span class='Bracket'>⟨</span> <span class='String'>"con"</span><span class='Separator'>,</span> <span class='String'>"cat"</span><span class='Separator'>,</span> <span class='String'>"enat"</span><span class='Separator'>,</span> <span class='String'>"e"</span> <span class='Bracket'>⟩</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oi+IOKfqCAiY29uIiwgImNhdCIsICJlbmF0IiwgImUiIOKfqQ==">↗️</a><pre> <span class='Function'>∾</span> <span class='Bracket'>⟨</span> <span class='String'>"con"</span><span class='Separator'>,</span> <span class='String'>"cat"</span><span class='Separator'>,</span> <span class='String'>"enat"</span><span class='Separator'>,</span> <span class='String'>"e"</span> <span class='Bracket'>⟩</span> "concatenate" </pre> <h2 id="example-base-decoding">Example: base decoding</h2> @@ -253,19 +253,19 @@ </table> <p>Almost. It's really close. There are just two things missing, so I'll cover those and can we agree one and three-quarters is pretty good? First is Range (<code><span class='Function'>↕</span></code>), which is called on a number to give all the natural numbers less than it:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oaVIDg=&run">↗️</a><pre> <span class='Function'>↕</span> <span class='Number'>8</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oaVIDg=">↗️</a><pre> <span class='Function'>↕</span> <span class='Number'>8</span> ⟨ 0 1 2 3 4 5 6 7 ⟩ </pre> <p>Natural numbers in BQN start at 0. I'll get to the second function in a moment, but first let's consider how we'd decode just one number in binary. I'll pick a smaller one: 9 is 1001 in binary. Like the first 1 in decimal 1001 counts for one thousand or <code><span class='Number'>10</span><span class='Function'>⋆</span><span class='Number'>3</span></code>, the first one in binary 1001 counts for 8, which is <code><span class='Number'>2</span><span class='Function'>⋆</span><span class='Number'>3</span></code>. We can put each number next to its place value like this:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=OOKAvzTigL8y4oC/MSDiiY3CqCAx4oC/MOKAvzDigL8x&run">↗️</a><pre> <span class='Number'>8</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span> <span class='Function'>≍</span><span class='Modifier'>¨</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=OOKAvzTigL8y4oC/MSDiiY3CqCAx4oC/MOKAvzDigL8x">↗️</a><pre> <span class='Number'>8</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span> <span class='Function'>≍</span><span class='Modifier'>¨</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span> ⟨ ⟨ 8 1 ⟩ ⟨ 4 0 ⟩ ⟨ 2 0 ⟩ ⟨ 1 1 ⟩ ⟩ </pre> <p>To get the value we multiply each number by its place value and then add them up.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8K0IDjigL804oC/MuKAvzEgw5cgMeKAvzDigL8w4oC/MQ==&run">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>´</span> <span class='Number'>8</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</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'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8K0IDjigL804oC/MuKAvzEgw5cgMeKAvzDigL8w4oC/MQ==">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>´</span> <span class='Number'>8</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</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'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span> 9 </pre> <p>Now we'd like to generate that list <code><span class='Number'>8</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span></code> instead of writing it out, particularly because it needs to be twice as long to decode eight-bit ASCII characters (where the first bit is always zero come on robots would never use such an inefficient format). It's the first four powers of two, or two to the power of the first four natural numbers, in reverse. While we're at it, let's get <code><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span></code> from <code><span class='String'>"1001"</span></code> by subtracting <code><span class='String'>'0'</span></code>. Nice.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiDii4Yg4oaVNAoK4oy9MuKLhuKGlTQKCijijL0y4ouG4oaVNCkgw5cgIjEwMDEiLScwJwoKK8K0ICjijL0y4ouG4oaVNCkgw5cgIjEwMDEiLScwJw==&run">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>⋆</span> <span class='Function'>↕</span><span class='Number'>4</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiDii4Yg4oaVNAoK4oy9MuKLhuKGlTQKCijijL0y4ouG4oaVNCkgw5cgIjEwMDEiLScwJwoKK8K0ICjijL0y4ouG4oaVNCkgw5cgIjEwMDEiLScwJw==">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>⋆</span> <span class='Function'>↕</span><span class='Number'>4</span> ⟨ 1 2 4 8 ⟩ <span class='Function'>⌽</span><span class='Number'>2</span><span class='Function'>⋆↕</span><span class='Number'>4</span> @@ -279,34 +279,34 @@ </pre> <p>Lot of functions up there. Notice how I need to use parentheses for the left argument of a function if it's compound, but never for the right argument, and consequently never with a one-argument function.</p> <p>Representing our ASCII statement as a list of lists, we convert each digit to a number as before:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=JzAnIC3LnCAiMDEwMDExMTAi4oC/IjAxMTAwMTAxIuKAvyIwMTExMDAxMCLigL8iMDExMDAxMDAi4oC/IjAwMTAwMDAxIg==&run">↗️</a><pre> <span class='String'>'0'</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>"01001110"</span><span class='Ligature'>‿</span><span class='String'>"01100101"</span><span class='Ligature'>‿</span><span class='String'>"01110010"</span><span class='Ligature'>‿</span><span class='String'>"01100100"</span><span class='Ligature'>‿</span><span class='String'>"00100001"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=JzAnIC3LnCAiMDEwMDExMTAi4oC/IjAxMTAwMTAxIuKAvyIwMTExMDAxMCLigL8iMDExMDAxMDAi4oC/IjAwMTAwMDAxIg==">↗️</a><pre> <span class='String'>'0'</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>"01001110"</span><span class='Ligature'>‿</span><span class='String'>"01100101"</span><span class='Ligature'>‿</span><span class='String'>"01110010"</span><span class='Ligature'>‿</span><span class='String'>"01100100"</span><span class='Ligature'>‿</span><span class='String'>"00100001"</span> ⟨ ⟨ 0 1 0 0 1 1 1 0 ⟩ ⟨ 0 1 1 0 0 1 0 1 ⟩ ⟨ 0 1 1 1 0 0 1 0 ⟩ ⟨ 0 1 1 0 0 1 0 0 ⟩ ⟨ 0 0 1 0 0 0 0 1 ⟩ ⟩ </pre> <p>Now we need to multiply each digit by the right place value, and add them up. The adding part is easy, just requiring an Each.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8K0wqggJzAnIC3LnCAiMDEwMDExMTAi4oC/IjAxMTAwMTAxIuKAvyIwMTExMDAxMCLigL8iMDExMDAxMDAi4oC/IjAwMTAwMDAxIg==&run">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>´¨</span> <span class='String'>'0'</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>"01001110"</span><span class='Ligature'>‿</span><span class='String'>"01100101"</span><span class='Ligature'>‿</span><span class='String'>"01110010"</span><span class='Ligature'>‿</span><span class='String'>"01100100"</span><span class='Ligature'>‿</span><span class='String'>"00100001"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8K0wqggJzAnIC3LnCAiMDEwMDExMTAi4oC/IjAxMTAwMTAxIuKAvyIwMTExMDAxMCLigL8iMDExMDAxMDAi4oC/IjAwMTAwMDAxIg==">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>´¨</span> <span class='String'>'0'</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>"01001110"</span><span class='Ligature'>‿</span><span class='String'>"01100101"</span><span class='Ligature'>‿</span><span class='String'>"01110010"</span><span class='Ligature'>‿</span><span class='String'>"01100100"</span><span class='Ligature'>‿</span><span class='String'>"00100001"</span> ⟨ 4 4 4 3 2 ⟩ </pre> <p>Multiplication is harder, and if we try to multiply by the place value list directly it doesn't go so well.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KOKMvTLii4bihpU4KSDDlyAnMCcgLcucICIwMTAwMTExMCLigL8iMDExMDAxMDEi4oC/IjAxMTEwMDEwIuKAvyIwMTEwMDEwMCLigL8iMDAxMDAwMDEi&run">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>⌽</span><span class='Number'>2</span><span class='Function'>⋆↕</span><span class='Number'>8</span><span class='Paren'>)</span> <span class='Function'>×</span> <span class='String'>'0'</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>"01001110"</span><span class='Ligature'>‿</span><span class='String'>"01100101"</span><span class='Ligature'>‿</span><span class='String'>"01110010"</span><span class='Ligature'>‿</span><span class='String'>"01100100"</span><span class='Ligature'>‿</span><span class='String'>"00100001"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KOKMvTLii4bihpU4KSDDlyAnMCcgLcucICIwMTAwMTExMCLigL8iMDExMDAxMDEi4oC/IjAxMTEwMDEwIuKAvyIwMTEwMDEwMCLigL8iMDAxMDAwMDEi">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>⌽</span><span class='Number'>2</span><span class='Function'>⋆↕</span><span class='Number'>8</span><span class='Paren'>)</span> <span class='Function'>×</span> <span class='String'>'0'</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>"01001110"</span><span class='Ligature'>‿</span><span class='String'>"01100101"</span><span class='Ligature'>‿</span><span class='String'>"01110010"</span><span class='Ligature'>‿</span><span class='String'>"01100100"</span><span class='Ligature'>‿</span><span class='String'>"00100001"</span> ERROR</pre> <p>This is because the list on the left has length 8 while the list on the right has length 5. The <em>elements</em> of the list on the right have length 8, but BQN can't be expected to know you want to connect the two arguments in that particular way. Especially considering that if you happen to have 8 characters then the right argument <em>will</em> have length 8!</p> <p>There are a few ways to handle this. What we'll do is <em>bind</em> the place values to <code><span class='Function'>×</span></code> using the 2-modifier <code><span class='Modifier2'>⊸</span></code>. This modifier attaches a left argument to a function.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ImFiIiDiiL7CqCDin6ggImNkIiwgInV0IiDin6kKCiJhYiLiirjiiL7CqCDin6ggImNkIiwgInV0IiDin6k=&run">↗️</a><pre> <span class='String'>"ab"</span> <span class='Function'>∾</span><span class='Modifier'>¨</span> <span class='Bracket'>⟨</span> <span class='String'>"cd"</span><span class='Separator'>,</span> <span class='String'>"ut"</span> <span class='Bracket'>⟩</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ImFiIiDiiL7CqCDin6ggImNkIiwgInV0IiDin6kKCiJhYiLiirjiiL7CqCDin6ggImNkIiwgInV0IiDin6k=">↗️</a><pre> <span class='String'>"ab"</span> <span class='Function'>∾</span><span class='Modifier'>¨</span> <span class='Bracket'>⟨</span> <span class='String'>"cd"</span><span class='Separator'>,</span> <span class='String'>"ut"</span> <span class='Bracket'>⟩</span> ⟨ "acd" "but" ⟩ <span class='String'>"ab"</span><span class='Modifier2'>⊸</span><span class='Function'>∾</span><span class='Modifier'>¨</span> <span class='Bracket'>⟨</span> <span class='String'>"cd"</span><span class='Separator'>,</span> <span class='String'>"ut"</span> <span class='Bracket'>⟩</span> ⟨ "abcd" "abut" ⟩ </pre> <p>In the first bit of code above, <code><span class='Modifier'>¨</span></code> matches up its left and right arguments. In the second, we bind <code><span class='String'>"ab"</span></code> to <code><span class='Function'>∾</span></code> first—remember that modifiers associate from left to right, so that <code><span class='String'>"ab"</span><span class='Modifier2'>⊸</span><span class='Function'>∾</span><span class='Modifier'>¨</span></code> and <code><span class='Paren'>(</span><span class='String'>"ab"</span><span class='Modifier2'>⊸</span><span class='Function'>∾</span><span class='Paren'>)</span><span class='Modifier'>¨</span></code> are the same—and Each only sees one argument. <code><span class='String'>"ab"</span></code>, packed inside Each's operand, is reused each time. The same principle applies to our binary problem:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8K0wqggKOKMvTLii4bihpU4KeKKuMOXwqggJzAnIC3LnCAiMDEwMDExMTAi4oC/IjAxMTAwMTAxIuKAvyIwMTExMDAxMCLigL8iMDExMDAxMDAi4oC/IjAwMTAwMDAxIg==&run">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>´¨</span> <span class='Paren'>(</span><span class='Function'>⌽</span><span class='Number'>2</span><span class='Function'>⋆↕</span><span class='Number'>8</span><span class='Paren'>)</span><span class='Modifier2'>⊸</span><span class='Function'>×</span><span class='Modifier'>¨</span> <span class='String'>'0'</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>"01001110"</span><span class='Ligature'>‿</span><span class='String'>"01100101"</span><span class='Ligature'>‿</span><span class='String'>"01110010"</span><span class='Ligature'>‿</span><span class='String'>"01100100"</span><span class='Ligature'>‿</span><span class='String'>"00100001"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8K0wqggKOKMvTLii4bihpU4KeKKuMOXwqggJzAnIC3LnCAiMDEwMDExMTAi4oC/IjAxMTAwMTAxIuKAvyIwMTExMDAxMCLigL8iMDExMDAxMDAi4oC/IjAwMTAwMDAxIg==">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>´¨</span> <span class='Paren'>(</span><span class='Function'>⌽</span><span class='Number'>2</span><span class='Function'>⋆↕</span><span class='Number'>8</span><span class='Paren'>)</span><span class='Modifier2'>⊸</span><span class='Function'>×</span><span class='Modifier'>¨</span> <span class='String'>'0'</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>"01001110"</span><span class='Ligature'>‿</span><span class='String'>"01100101"</span><span class='Ligature'>‿</span><span class='String'>"01110010"</span><span class='Ligature'>‿</span><span class='String'>"01100100"</span><span class='Ligature'>‿</span><span class='String'>"00100001"</span> ⟨ 78 101 114 100 33 ⟩ </pre> <p>To wrap things up, we just convert from numbers to characters by adding the null character.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=QCArICvCtMKoICjijL0y4ouG4oaVOCniirjDl8KoICcwJyAty5wgIjAxMDAxMTEwIuKAvyIwMTEwMDEwMSLigL8iMDExMTAwMTAi4oC/IjAxMTAwMTAwIuKAvyIwMDEwMDAwMSI=&run">↗️</a><pre> <span class='String'>@</span> <span class='Function'>+</span> <span class='Function'>+</span><span class='Modifier'>´¨</span> <span class='Paren'>(</span><span class='Function'>⌽</span><span class='Number'>2</span><span class='Function'>⋆↕</span><span class='Number'>8</span><span class='Paren'>)</span><span class='Modifier2'>⊸</span><span class='Function'>×</span><span class='Modifier'>¨</span> <span class='String'>'0'</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>"01001110"</span><span class='Ligature'>‿</span><span class='String'>"01100101"</span><span class='Ligature'>‿</span><span class='String'>"01110010"</span><span class='Ligature'>‿</span><span class='String'>"01100100"</span><span class='Ligature'>‿</span><span class='String'>"00100001"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=QCArICvCtMKoICjijL0y4ouG4oaVOCniirjDl8KoICcwJyAty5wgIjAxMDAxMTEwIuKAvyIwMTEwMDEwMSLigL8iMDExMTAwMTAi4oC/IjAxMTAwMTAwIuKAvyIwMDEwMDAwMSI=">↗️</a><pre> <span class='String'>@</span> <span class='Function'>+</span> <span class='Function'>+</span><span class='Modifier'>´¨</span> <span class='Paren'>(</span><span class='Function'>⌽</span><span class='Number'>2</span><span class='Function'>⋆↕</span><span class='Number'>8</span><span class='Paren'>)</span><span class='Modifier2'>⊸</span><span class='Function'>×</span><span class='Modifier'>¨</span> <span class='String'>'0'</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='String'>"01001110"</span><span class='Ligature'>‿</span><span class='String'>"01100101"</span><span class='Ligature'>‿</span><span class='String'>"01110010"</span><span class='Ligature'>‿</span><span class='String'>"01100100"</span><span class='Ligature'>‿</span><span class='String'>"00100001"</span> "Nerd!" </pre> <p>Was it as anticlimactic as you'd hoped? In fact there's a simpler way to do the base decoding as well, using <code><span class='Modifier2'>⊸</span></code>'s mirror image <code><span class='Modifier2'>⟜</span></code> in a different way. We'll discuss that in the next tutorial!</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8K0ICjijL0y4ouG4oaVNCkgw5cgIjEwMDEiLScwJwor4p+cKCvLnCnCtCDijL0gIjEwMDEiLScwJw==&run">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>´</span> <span class='Paren'>(</span><span class='Function'>⌽</span><span class='Number'>2</span><span class='Function'>⋆↕</span><span class='Number'>4</span><span class='Paren'>)</span> <span class='Function'>×</span> <span class='String'>"1001"</span><span class='Function'>-</span><span class='String'>'0'</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8K0ICjijL0y4ouG4oaVNCkgw5cgIjEwMDEiLScwJwor4p+cKCvLnCnCtCDijL0gIjEwMDEiLScwJw==">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>´</span> <span class='Paren'>(</span><span class='Function'>⌽</span><span class='Number'>2</span><span class='Function'>⋆↕</span><span class='Number'>4</span><span class='Paren'>)</span> <span class='Function'>×</span> <span class='String'>"1001"</span><span class='Function'>-</span><span class='String'>'0'</span> 9 <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='Modifier'>´</span> <span class='Function'>⌽</span> <span class='String'>"1001"</span><span class='Function'>-</span><span class='String'>'0'</span> 9 |
