[REBOL] Re: Multiple Word Conditional?
From: rebol:techscribe at: 16-Nov-2000 15:16
Hi Rodney
if returns none if the condition is false, otherwise if's body is
evaluated and the result of evaluatiing the final expression in this
block is returned (if any). I,.e
>> if false [ "ok" ]
== none
>> if true ["ok"]
== "ok"
>> if true []
>>
In the last scenario the block did not return a value, and therefore
there was no return value for this expression. In summary, an if
expression is only guaranteed to return a value if it fails, namely the
value none. If the condition evaluates to true, then the result is
determined by the expression contained in the block.
The word any evaluates the sequentially evalutes the expressions
contained in its block as long as the the expressions evaluate to false
logic value. (or the block is exhausted).
Caution:
If I generalize your example
>either one [print "1"] [either two [print "2"][print "3"]]
then you want the expressions associated with two (or three) to be
evaluated only if the condition for the first expression fails.
But
>> amy [ if condition-1 [expression-1] if condition-2 [expression-2 ]
]
is not identical to
>> either condition-1 [expression-1] [ if condition-2 [expression-2] ]
In the case of any, if expression -1 evaluates to a logical false value
then condition-2 will be tested, even though condition-1 evaluated to
true. Using either, however, expression-2 will only be evaluated if
condition-1 failed and condition-2 succeeds.
>> if true [false]
== false
>> any [ if true [false] if true [print "second expression"]
== second expression.
In the either case the "second expression" string will not be printed.
>> either true [false] [print "second expression"]
== false
In short
any: expression-2 will be evaluated in two cases:
(1) condition-1 fails and condition-2 succeeds.
(2) condition-1 succeeds, expression-1 returns a logical false value,
and condition-2 succeeds.
either: expression-2 will be evaluated only in case
(1) condition-1 fails and condition-2 succeeds.
There is no second case here.
That's a subtle difference that may cause some headscratching, if you
don't account for it in your algorithm.
Hope this helps,
Elan