|REBOL 3 Docs||Guide||Concepts||Functions||Datatypes||Errors|
|TOC < Back Next >||Updated: 19-Apr-2009 Edit History|
Code is data, grouped in blocks, then evaluated.
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 3
print length? "example" 7
print 1 + length? "example" 8
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.
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.
if 10 > 4 [print 1 + 2] 3
The if function evaluates the block, because its conditional expression is true.
loop 3 [print 1 + 2] 3 3 3
The example code:
print 1 + 2 3
could also be written as simply:
1 + 2 3
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] 3
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] 3
What happens when the conditional is not true? Read the next section.
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] none
As we said above, every block returns a result. The above line is actually:
do [if 4 > 10 [print 1 + 2]] none
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.
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 3
loop 3 blk 3 3 3
print length? blk 4
first blk print
last blk 2
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 >||REBOL.com - WIP Wiki||Feedback Admin|