Evaluation with do and reduce

In the previous chapter, we saw how to use do to evaluate a script in the console with do %script.red.

In fact, do is more general in that it accepts any expression, function, or block, such as here:

;-- see Chapter03/evaluating.red:
do 9 * 9 ;== 81
do square-root 25 ;== 5.0
do [print [tab uppercase "Red"]] ; == "RED"

You can even use do %script.red from inside a Red program, that's how powerful it is. Let's make script.red containing—Red[] print "This is executed in a script". Now edit an evaluating.red program containing—Red[] do %script.red.

Then red evaluating.red will print the text, This is executed in a script in the console. To see the same output in a terminal compile it with red -r evaluating.red and then type the command ./evaluating.

The reduce word evaluates expressions inside a block and returns a new block with the evaluated values, such as in this example:

bl: [8 * 3 99 / 11 square-root 25 [pi ** 2]] 
r: reduce bl ;== [24 9 5.0 [pi ** 2]]
bl ;== [8 * 3 99 / 11 square-root 25 [pi ** 2]]

You see that reduce does not evaluate nested blocks, and the original block remains unchanged.