REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 19-Apr-2009 Edit History  

REBOL 3 Guide: Code: Evaluating blocks

Code is data, grouped in blocks, then evaluated.


Evaluation, not execution

REBOL evaluates blocks of values and words to produce one or more results.

We use the term evaluate rather than execute. When using REBOL, think about computing values instead of executing steps or instructions. REBOL is a functional language.

Here are some examples of evaluating blocks of values and words:

print 1 + 2
print length? "example"
print 1 + length? "example"

In those lines the values are 1, 2, and "example", and the words are: print, length?, and +. Those words are associated with functions that perform various computations and return results. For instance, above, length? returns the length of the string.

Code is held in blocks

When you type a line into the console:

print 1 + 2

you are actually creating the code block:

[print 1 + 2]

and that block gets evaluated to produce the result.

Although the brackets are not required for your line when you type it, it is implied: the line is a block.

In fact, when you type that line at the console, it is a shortcut for:

do [print 1 + 2]

This is important to realize, because you use blocks in a similar way for all code.

For example:

if 10 > 4 [print 1 + 2]

The if function evaluates the block, because its conditional expression is true.

Another example:

loop 3 [print 1 + 2]

The loop function is just like do, except it evaluates the block three times here.

Blocks return results

The examples above all use print to display their results. That was done to make them clear, but the print hides an important fact: when evaluated, every block returns a result.

The example code:

print 1 + 2

could also be written as simply:

1 + 2

We see the 3 here because the console automatically prints all resulting values.

But, in more precise terms, this is true too:

do [1 + 2]

The result of blocks is often used in code, and you will be seeing it in many examples ahead.

For example, take special note of this case:

if 10 > 4 [1 + 2]

What happens when the conditional is not true? Read the next section.

Blocks aren't always evaluated

Blocks are not always evaluated. In many cases blocks may be skipped, due to the result of an expression.

For example, in this case:

if 4 > 10 [print 1 + 2]

no result is printed. The block is not evaluated because the if condition is false.

Or, if you wrote:

loop 0 [print 1 + 2]

Nothing happens. The block is never evaluated.

Actually, it turns out, if you enter that line into the console, you will see:

if 4 > 10 [print 1 + 2]

As we said above, every block returns a result. The above line is actually:

do [if 4 > 10 [print 1 + 2]]

so, the result of if is none when the condition is false. The importance of this feature will become more clear once you learn a bit more about the language.

Code is data and data can be code

In REBOL, blocks are used for both code and data. In fact, code is just data.

This is a key concept of REBOL that you must keep in mind:

Starting with the example above, now look at what you can do:

blk: [print 1 + 2]

if 10 > 4 blk
loop 3 blk
print length? blk
first blk
last blk
reverse blk
[2 + 1 print]

This concept makes the language extremely flexible, and as you will see later, gives it great power many expressions with very little code. The importance of this concept will become a lot more clear as you learn more about the language.

  TOC < Back Next > - WIP Wiki Feedback Admin