Hi Piroz,


>In this context, what is the definition of a
>"condition" 

A condition is an expression that evaluates either to a logical false
(including none, off) or something else. Like in C, everything that is not
a logical false is considered true, i.e. 
>> if 1 [print {will be evaluated because 1 is not logical false}]
will be evaluated because 1 is not logical false
>> if [] [print {will be evaluated because an empty block, [], is not
logical false}]
will be evaluated because an empty block, [], is not logical false

>and why does
>REBOL treat the two conditions differently?  

The condition expression for if needs to be evaluated exactly once. First,
we evaluate the condition expression and then - depending on what this
expression evaluates to - we either do or do not evaluate if's body. What
happens in that body cannot affect the result of having evaluated the
condition expression. There is therefore no reason to pass the condition
expression on to if for evaluation. The condition expression will be
evaluated in the context in which if is called, and the resulting value
will be passed to if. 

In contrast, while's condition has to be evaluated repeatedly, once on
entering while and repeatedly after while's body has been evaluated.
Therefore, "while" itself must evaluate its condition expression. It cannot
simply receive the result of a single evaluation of the condition epxression. 

To protect while's condition expression from being evaluated in the context
in which while is being called (i.e. the global context of the REBOL shell,
or the context of a function in which while is being called), the condition
expression is embedded in a block. The condition expression block can
contain as many expressions as required. The condition expression itself -
not the result of having evaluated it - is passed to while in a block and
and while will evaluate the condition expression as often as it needs to.

We expect that some expression evaluated in while's body will eventually
affect the condition expression and that expression will return false. The
condition expression must therefore be able to reflect modifications that
occurred as a result of evaluating while's body.


>Why not
>use a block for an
>"if" statement as we do for loops?

There are two factors that determine that we must pass the condition
expression unmodified - i.e. embedded in a block - to while: the condition
expression must be evaluated repeatedly, and during its repeated evaluation
changes that occured within while's body must be able to affect values that
are processed in the condition expression. 

Neither of these factors apply to if. There is no reason to complicate if's
implementation by forcing it to evaluate its condition expression, when
that expression can be safely evaluated before the result of that
evaluation is passed to if.



;- Elan >> [: - )]

Reply via email to