From e2b07a5fd0bbaad232c717fb90a31d6c61d72bd4 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Thu, 14 Jul 2022 20:06:50 -0400 Subject: Try to include previous variable definitions in REPL links --- docs/tutorial/variable.html | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'docs/tutorial/variable.html') diff --git a/docs/tutorial/variable.html b/docs/tutorial/variable.html index 1bb916ac..5e05d336 100644 --- a/docs/tutorial/variable.html +++ b/docs/tutorial/variable.html @@ -24,11 +24,11 @@ ⟨ 3 7 ⟩

A variable can't be defined twice in the same scope. Later we'll work with functions and other pieces of code that create their own scopes, but for now all you need to know is that all the code in a tutorial runs in the same scope. So three is already defined, and can't be defined again.

-↗️
    three  4
+↗️
    three  4
 Error: Redefinition
 

It's a little crazy to call them variables if the definition can never change, right? Doesn't "variable" mean "able to change"? Fortunately, this is one way in which BQN isn't crazy. You can modify a variable's value with the arrow provided it's already been defined. This never does anything to the original value: that value stays the same; it's just (probably) not the value of the modified variable any more.

-↗️
    three  4
+↗️
    three  4
 
     three = 3   # Wait why did I do that
 0
@@ -46,7 +46,7 @@
 

Does BQN not know about capital letters? Does it object to self-reference? Why is "BQN" green? At least there's an error message, and a "role" is something we've heard about before. Assignment means anything written with a leftward arrow—either definition or modification.

I'll first confuse you a little more by pointing out that BQN's variables are case-insensitive, and even underscore-insensitive!

-↗️
    three
+↗️
    three
 3
     thrEe
 3
@@ -60,7 +60,7 @@
 3
 

But the syntax highlighter still seems to care, and you'll get a strange result if you try to apply a function to one of the uppercase spellings:

-↗️
    - Three
+↗️
    - Three
 -3{𝔽}
 
     - _three
@@ -118,7 +118,7 @@
 

This strategy allows us to break down a program into smaller parts. However, you can only name a function in this way, not an expression. We'll explain later how to turn an expression into an explicit function. But one thing remains true regardless of how a function is created: functions are just another kind of BQN value, and giving a function a name uses the ordinary definition arrow , not any special syntax.

Even if you define a variable to be a function at first, you're not locked in to that choice. You can modify the variable to have a different value (but remember to change the casing to match the new value's role!). If it's a data value, you'll still be able to call it as a function: it will return itself.

-↗️
    Base2
+↗️
    Base2
 +⟜(2⊸×)´∘⌽
 
     base2  16   # Change it to a number
@@ -312,7 +312,7 @@
 ⟨ 4 5 6 ⟩
 

But this changes the value of a to a completely unrelated value. What if I want to apply a transformation to a, for example to subtract one? Of course I can write the value a on the right hand side of the assignment, but that's extra work and doesn't really seem to represent what I'm doing, which is conceptually just to apply a function to a. So BQN also has a shorthand, called modified assignment. Here, the modified assignment - subtracts a value from a.

-↗️
    a  a - 1
+↗️
    a  a - 1
     a
 ⟨ 3 4 5 ⟩
 
@@ -320,31 +320,31 @@
 ⟨ 2 3 4 ⟩
 

(In case you're wondering why I didn't have to write a again that last time, the evaluator suppresses the printed result for ordinary assignments but not modified ones. This is a feature of my website software and not the BQN language). It looks a lot like the special assignment operators +=, /=, and so on that you'll see in C or Javascript. What BQN brings to the table is that you can use any two-argument function at all here, because two-argument function are always written as operators. For example, we can prepend some elements to a:

-↗️
    a ˜ 01
+↗️
    a ˜ 01
 ⟨ 0 1 2 3 4 ⟩
 

But what about functions with only one argument? It's possible to do this using a dummy right argument such as the null character, @. To turn a function that takes one argument into one that takes two, we can compose it with a function that takes two arguments and returns one of them. The left one, since the variable to modify is on the left hand side. Perhaps… Left? ()?

-↗️
    "abcd"  "wxyz"
+↗️
    "abcd"  "wxyz"
 "dcba"
 
     a  @
 ⟨ 4 3 2 1 0 ⟩
 

But fortunately, there's a simpler syntax as well: write your one-argument function before with no right hand side. Bit of a Yoda vibe: "a reversed is".

-↗️
    a 
+↗️
    a 
 ⟨ 0 1 2 3 4 ⟩
 
     a 4-           # And back again
 ⟨ 4 3 2 1 0 ⟩
 

Notice that there's no need for parentheses: modifiers bind more strongly than the assignment character. Now what if we want to decrease the last two elements of a? That is, we want to compute the following array while storing it in a.

-↗️
    -4(¯2) a
+↗️
    -4(¯2) a
 ⟨ 4 3 2 ¯3 ¯4 ⟩
 
     a                # It hasn't changed, of course
 ⟨ 4 3 2 1 0 ⟩
 

The code to do this looks the same as what we did with Reverse (). Again we don't have to parenthesize the function, because modifiers associate from left to right, so Under () binds to its operands before Compose () does.

-↗️
    a -4(¯2)
+↗️
    a -4(¯2)
 ⟨ 4 3 2 ¯3 ¯4 ⟩
 
-- cgit v1.2.3