[REBOL] Re: Need help printing first elm of block
From: joel:neely:fedex at: 21-May-2001 10:20
Hi, Charles,
Charles Wardell wrote:
> Please help me understand what is happening here.
>
> >> aVar: "one"
> == "one"
> >> bVar: "two"
> == "two"
> >>
> >> blk: [aVar bVar]
> == [aVar bVar]
> >>
> >> print blk
> one two <----------------------- Great!!!
> >>
>
> >> print first blk
> aVar <-------------------- Why?
> >>
Because PRINT does an implicit REDUCE on the argument block.
BLK contains two WORD! values, but if you force evaluation,
each of those words is bound to a string.
>> foreach item blk [print [type? :item tab item]]
word aVar
word bVar
>>
If PRINT didn't do the implicit REDUCE, then you wouldn't
be able to put expressions inside a block argument to PRINT
and get the values of those expressions as output without
explicitly forcing the reduction yourself. Since it's most
likely that we really want such expressions evaluated, the
current policy is a net win.
>> caption: "The answer is:"
== "The answer is:"
followed by
>> print [caption 2 + 2]
The answer is: 4
>> print-without-reducing [caption 2 + 2]
[caption 2 + 2]
I suspect that most of the time we'd expect the first output
rather than the second. (I cheated... Here's the missing
definition:
>> print-without-reducing: func [b [block!]] [print mold b]
in case you were interested.)
So, the next question is "Which do you want?" REBOL lets you
decide whether you want the evaluation, but that means you have
to specify which you want. If you do want reduction for the
items in the block, you can use any of the following:
>> print get first blk
one
>> print first reduce blk
one
On the other hand, if you want to suppress evaluation of the
entire block, you can use any of these:
>> print [blk]
aVar bVar
>> print mold blk
[aVar bVar]
Ya payz ya money, ya getz ya choyse!
-jn-