Word bindings

Within a Red program, several contexts can exist. A context is a scope in which a word can be defined, or as reducers say—a word is bound to a context. For example, within a function context a local word price may be used, and in another function the same word price may have a totally different meaning or value—the word price is used in different contexts.

Other types of contexts are within a block or within the entire script or environment (the global context). For example, if you put in type ? chocolate in the Red console, it prints the following:

No matching values were found in the global context.

Within a context, a word refers to a value, as in these examples:

;-- see Chapter03/bindings.red:
age: 62
is-integer?: false
lunch-time: 12:32
birth-day: 17-Jan-1997
friends: ["Jeff" "Paula" "Viviane"]

The preceding words are bound to the global context, and refer to the values mentioned. For example, the age word here exists in the global context and refers to the integer value 62, the same goes for the word friends, which refers to the block value ["Jeff" "Paula" "Viviane"].

The general syntax for a word var referring to a value val is:

var: val

No terminator (such as a semicolon) is required at the end, but there has to be a space between the : and the value.

It is important to note that the : is not an assignment operator such as in most other programming languages. It only binds the word before it to the result of the expression following the :. In Red, var: has its own type called set-word!

var simply returns the value, :var, which is called getting the value of the word var, has the same effect, as is shown with the variable age:

age     ;== 62  <-- remember: this comment shows the value of age
:age ;== 62

As with var:, :var has its own type called get-word!.

If it doesn't harm readability, you can place bindings on one line:

n: 12   m: 42

Multiple variables can be set to a single value in one line, like this:

age: number: size: 62

You can also use set combined with a block to give a number of words the same value:

set [log-time start-time run-time] 10:57  ;== 10:57:00
start-time ;== 10:57:00
log-time ;== 10:57:00
run-time ;== 10:57:00

age, is-integer?, lunch-time, birth-day, and friends are all words. Words are the nearest thing Red has to variables from other programming languages; that's why we will sometimes speak of variables. But keep in mind—they are words that refer to values.

To check if a word has a value, use the value? function:

value? age  ;== true

⇒ Now answer question 3 from the Questions section.

Use print var or prin var to output the formatted value of a variable var (with or without a newline) in a script. Use probe var to view the raw data in its type notation, which is useful for debugging, as are ? and ??:

print friends ;== Jeff Paula Viviane
probe friends ;== ["Jeff" "Paula" "Viviane"]
? friends ;== FRIENDS is a block! value. length: 3 ["Jeff" "Paula" "Viviane"]
?? friends ;== friends: ["Jeff" "Paula" "Viviane"]

⇒ Now answer question 4 from the Questions section.

It is useful to mention here two other related functions:

  • form returns a user-friendly string representation of a value, such as the following:
      form friends        ;== "Jeff Paula Viviane"

As you can see, it generates regular text from a block, so it's mostly used in string and text manipulation.

  • mold returns a source format multiline string representation of a value:
      mold friends        ;== {["Jeff" "Paula" "Viviane"]}

Its basic usage is to produce Red text that can be saved and re-loaded to recreate the original values.

When an output involves many parts, print is often used with a block, as follows:

name: "John"
print ["My name is:" name] ;== My name is: John

The parts are automatically spaced. Special names exist for certain characters, such as tab, newline (also as character #"^/"), lf (linefeed), and more.

If you want to know the type of a variable, use type?:

type? age         ;== integer!
type? birth-day ;== date!
type? friends ;== block!
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.161.225