Once we start to add values to our block, we can start to see things differently. The INSERT function adds a value to a series at the current position and returns the position after the inserted value:

>> block-after-insert: insert block 1234

== []

This can be a little disconcerting as it appears that nothing has happened. Let’s review our values though:

>> block

== [1234] >> index? block

== 1 >> index? block-after-insert

== 2 >> equal? block block-after-insert

== false

It’s clear then that something has happened, our block BLOCK indeed contains a new value. How do we understand BLOCK-AFTER-INSERT then?

BLOCK and BLOCK-AFTER-INSERT both reference the same block, but at different positions. At our disposal are a few functions that allow us to change positions: NEXT, BACK and SKIP (amongst others):

>> back block-after-insert

== [1234] >> index? back block-after-insert

== 1 >> equal? block back block-after-insert

== true >> next block

== [] >> index? next block

== 2 >> equal? skip block 1 block-after-insert

== true >> equal? block skip block-after-insert -1

== true

With these three functions, we can traverse a series based on our current position.

It’s also worth noting that LENGTH? is relative to the current position:

>> length? block

== 1 >> length? block-after-insert

== 0

Heads or Tails

If the preceding functions can step through a series relative to the position within the series, then HEAD and TAIL will take you respectively and absolutely to the beginning or current end of a series:

>> block: [one two three four]

== [one two three four] >> block-tail: tail block

== [] >> block-head: head block-tail

== [one two three four]

Again, even though BLOCK-TAIL appears to be empty, it references the same block as BLOCK, just at a different position. One the one hand, the appearance of being empty is misleading, on the other, some functions don’t differentiate between an empty block and a block at it’s tail. As an example, iterators such as FOREACH only operate on a block from its current position forward.

>> foreach item next [1 2 3] [probe item]

2

3

We can combine operators to get to a specific position — in this case, last but one:

>> back tail block

== [four]

In addition to the functions that take you to the head or tail of a series, there are functions to test whether we are at the head or tail:

>> head? block

== true >> tail? block

== false >> head? block-head

== true >> tail? block-tail

== true

It’s important to note though that our reference to a series at a position is numeric and that a change in the series prior to the reference does not move the reference accordingly:

>> index? block-tail

== 5 >> insert block 'zero

== [one two three four] >> block

== [zero one two three four] >> index? block-tail

== 5 >> block-tail

== [four] >> tail? block-tail

== false

Take your Pick

To recover values from a series, there is the PICK function. PICK takes a numerical argument and recovers the appropriate item relative to the current position. PICK is also one-based:

>> block: skip [one two three four] 2

== [three four] >> pick block 1

== three >> pick block 2

== four

Picking from beyond the tail of a series returns none:

>> pick block 10

== none

Negative values will work where the series is not at the head.

>> pick block -1

== one >> pick block 0

== two

IMPORTANT NOTE: This behaviour is different in Rebol 2:

>> pick block -1

== two >> pick block 0

== none

Don’t Try to Change Me!

To alter the content of a series, we can use the INSERT, CHANGE, POKE and REMOVE functions.

As mentioned before, INSERT adds a value to the series at the current position and returns the position after the inserted value:

>> colors: [red green]

== [red green] >> insert next colors 'orange

== [green] >> colors

== [red orange green]

To add a value at the tail of a series, APPEND is shorthand for HEAD INSERT TAIL:

>> append colors 'blue

== [red orange green blue]

Ordinarily if you try to insert a block into a block, it will insert the contents of the inserted block:

>> insert tail colors [indigo violet]

== [] >> colors

== [red orange green blue indigo violet]

To insert a block, the /ONLY refinement should be used:

>> insert/only colors [black white]

== [red orange green blue indigo violet] >> colors

== [[black white] red orange green blue indigo violet]

To modify a value, we use CHANGE. CHANGE replaces the value at the current position with a new one and returns the series position after the changed value:

>> change colors 'silver

== [red orange green blue indigo violet] >> colors

== [silver red orange green blue indigo violet]

To replace multiple values, using a block as a parameter will change the next X values at the current position where X is the length of the block containing new values:

>> change colors [gold scarlet amber]

== [green blue indigo violet] >> colors

== [gold scarlet amber green blue indigo violet]

To replace a certain number of values regardless of the length of the block containing new values, we have a /PART refinement:

>> change/part colors [infrared red orange yellow cyan] 3

== [green blue indigo violet] >> colors

== [infrared red orange yellow cyan green blue indigo violet]

To replace the next value with a single block value, there is an /ONLY refinement similar to that of INSERT:

>> change/only colors [black white]

== [red orange yellow cyan green blue indigo violet] >> colors

== [[black white] red orange yellow cyan green blue indigo violet]

To replace a single value at a given offset, we have the POKE function. POKE returns the inserted value.

>> poke colors 1 'infrared

== infrared >> colors

== [infrared red orange yellow cyan green blue indigo violet]

Finally, to remove items from a series, we have the REMOVE function:

>> remove colors

== [red orange yellow cyan green blue indigo violet] >> remove skip colors 3

== [green blue indigo violet] >> colors

== [red orange yellow green blue indigo violet]

To remove multiple values, REMOVE has a /PART refinement:

>> remove/part next colors 3

== [blue indigo violet] >> colors

== [red blue indigo violet]

CLEAR can be used to remove all values from the current position to the tail:

>> clear next colors

== [] >> colors

== [red]

When used in conjunction with HEAD, you can clear out the whole series:

>> clear head insert [] 'red

== []

TAKE will remove the first value and return that value:

>> take colors

== red >> colors

== []

Manipulating Evaluation

Using just these basic accessors, it is possible to manipulate code on the fly:

>> code: []

== [] >> append code "Foo"

== [“Foo”] >> insert code 'print

== ["Foo"] >> probe code

== [print "Foo"] >> do code

Foo >> if true code

Foo

This is certainly a feature that makes Rebol stand out: everywhere a block is used—even in evaluation—that block can be manipulated prior to use.

Strings

Strings are somewhat beyond the scope of this article, save to say that strings are also series and many of the series operators work on strings as well: