[REBOL] Block Creation Re:
From: rebol:techscribe at: 18-Oct-2000 10:49
Hi Carl,
Anton and Brett already pointed out the difference. I merely want to add
that in the first case
>> block-1: []
you are assigning the word block as a reference to a LITERAL block. At
each loop iteration the word block-1 is being assigned to that same
LITERAL block.
Case 2:
>> block-2: to-block ""
Better yet:
>> block-2: to-block []
Here to-block creates a new block PROGRAMMATICALLY at each iteration
using the LITERAL block [] as a prototype. The prototype block remains
empty. The values are inserted into the empty PROGRAMMATICALLY (or
DYNAMICALLY) created block, not into its LITERAL prototype.
So,
>> repeat i 3 [block-1: [] insert block-1 i]
is the programmatic equivalent of
>> block-1: [3 2 1]
whereas
>> repeat i 3 [ block-2: to-block [] insert block-2 i]
is the programmatic equivalent of
>> block-2: [1]
>> block-2: [2]
>> block-2: [3]
A combination of the two allows you to do something like this:
repeat i 3 [
block-2: to-block block-1: []
append block-1 i
repeat j 3 [
append block-2 j
]
]
Here we modify the literal prototype block in the outer loop, and then
modify the result ing programmatically created block in the second loop.
Hope this sheds a little more light on how to utilize the difference.
Elan
[carl--cybercraft--co--nz] wrote: