Hi -t,
you wrote:
>thanks, I'm digesting this.
>I tend to put commands in the wrong order.  isn't first parse (or would
>that be 'first parse for the list?)--wouldn't that happen last?  how can
>I remember the correct order of things? think in reverse?
>

Keep in mind that everything in REBOL is a function that expects zero or
more arguments.

Accordingly, each function has to receive the data it is supposed to
operate on as an argument. Since REBOL is evaluated left-to-right, the
function that will do the final processing has to be all the way on the
left, then comes the function that prepares the data for processing by that
function, etc.

i.e. if you have the following word:

processed-data

and we want that data to be the result of reading a file we would say

processed-data: read %file.txt

However, if we want to first parse the file and assign processed-data as a
reference to the result parsing the file, parse has to receive read's
output as its input and original-data has to be assigned to the return
value of parse. 

We could introduce a temporary word to hold the return value of read
%file.txt and use it as an argument for parse:

temporary-data: read %file.txt

Now parse will be called like this:

processed-data: parse temporary-data none

with none being the second argument to parse, which controls which tokens
parse identifies and what it does with identified tokens. In this example
we use none, which tells parse to identify a space as a token separaptor
and return a block containing each token as a string.

REBOL does not require us to use a temporary word to store the data. We can
replace the word temporary-data above by the expression that was used to
populate temporary-data, which was read %file.txt:

processed-data: parse read %file.txt none

Perhaps the structure becomes clearer, when we use parentheses:

processed-data: parse (read %file.txt) none

The logic continues along the same lines. If I don't know what the name of
the file is, and I will be retrieving the name from the user, I could
assign filename like this:

filename: to-file ask "Please enter the filename: "

processed-data: parse (read filename) none

Again, REBOL does not require that I use the word filename as an
intermediate storage, I can replace filename by the instructions to
retrieve the filename from the user:

processed-data: parse (read (to-file ask "Please enter the filename: ") ) none

Again the parentheses identify the code fragments that create the argument
passed to the function outside the parentheses. How does REBOL evaluate this?

REBOL first finds the set-word! processed-data: When a set-word! value is
evaluated, REBOL tries to create a reference between the values' symbol and
some value. Here REBOL will try to assign processed-data as a reference to
some value. Which value? REBOL looks to the right of the set-word! to find
a value. There it finds parse. REBOL begins evaluating parse and determines
that parse is a function that requires two arguments. REBOL looks to the
right of parse to find the first argument. It finds read. Read again is a
function, which requires one argument. REBOL now tries to find an argument
for read and comes upon ask. Ask is evaluated with its argument "Please
enter the filename: ". Ask's return value is passed to read as its
argument. read reads the file and returns a string which is used as parse's
first argument. Now REBOL looks for parse's second argument. Everything
contained in parentheses was already evaluated and the next available token
is none. So, none is passed to parse as its second argument. Parse parses
the content of the file and returns a block. processed-data is set as a
reference to the block returned by parse. 

REBOL doesn't require the parentheses to group the code (in this
situation). So the same thing can be written without parentheses:

processed-data: parse read to-file ask "Please enter the filename: " none

Now that looks a little more confusing, right?

Hope this helps,

Elan

Reply via email to