[REBOL] Re: Why do 'append and 'repend return 'head series?
From: joel:neely:fedex at: 10-Aug-2002 9:09
Hi, Andrew,
Andrew Martin wrote:
> Why do 'append and 'repend return the head of the series?
>
...
> Would it be better for 'append and 'repend to return the 'series,
> instead of the 'head of the 'series?
>
...
I have a conjecture on that, but let me define my terms first, for
the sake of economy. (Forgive me in advance if I over-explain...)
Functional languages encourage one to think in terms of the value
an expression yields upon evaluation.
Imperative languages encourage one to think in terms of changes to
the state of the system (computer, data, I/O, etc.) that result
from executing commands.
REBOL does both. I have concluded that I must think of *both* the
value
of an expression and its "effect" to understand it fully.
(I say "effect" instead of "side effect" because the latter term
is used by functionally-minded cultures in a pejorative sense.)
With those background ideas...
The effect of APPEND, REPEND, INSERT, etc. is to mutate the
sequence of values underlying a SERIES! argument. We can use
these functions (as with many aspects of REBOL) in a very
imperative-like style. However, REBOL presents itself as an
Expression-Based language, which encourages us to use the value
resulting from an evaluation in a larger expression. This means
(for example) that I can "chain" a group of evaluations together
as in
append append append "fee" ", fie" ", foe" ", fum"
where the result of the rightmost APPEND is suitable as an
argument to the pending one to its left, and so on. In fact,
for this kind of use, APPEND could have returned a series
referencing *any* position within the sequence, as the pending
APPEND still goes to the end.
OTOH, if I wish to chain INSERT expressions, as in
insert insert insert next find "ducks:" "Huey" "Dewey" "Louie"
it is important for the rightmost INSERT to return a series that
is positioned immediately past the newly-inserted value, so that
successive INSERTions deposit values in the same order that we
can see from the original expression.
In short, the value (independently of the effect) appears to be
chosen with a view to suitability for what might be useful within
a larger expression. One might imagine that a natural thing to
do following an APPEND would be to do another, or to check the
length of the accumulating collection, in which case the total
sequence is relevant. Just a guess.
> After all, if one has set the position in a series explicitly,
> why should a function return the series at a different position?
>
Let's distinguish between "value" and "effect" again. In the
case of:
foo: somepositionwithin someseries
append foo bletch
or (more expressionally):
append foo: somepositionwithin someseries bletch
the effect is what we would expect and the value is irrelevant.
Our expression makes it clear that FOO still refers to the same
position after the APPEND has done its work.
If OTOH we had written
foo: somepositionwithin someseries
foo: append foo bletch
or (more expressionally):
foo: append somepositionwithin someseries bletch
then our expression makes it clear that we want FOO to be set
based on the *value* of APPEND *after* the *effect* of APPEND
has been accomplished.
So, REBOL lets us have our cake and eat it too (but we have to
choose on a bite-by-bite basis! ;-)
Just my $0.02 worth of conjecture...
-jn-
--
; Joel Neely joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]