Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Newbie Q: Cropping text

From: brett:codeconscious at: 8-Jul-2002 18:53

Hi Charles,
> > > Personally, I have a hard time working with > > > series! with next and skip and tail and so forth. > > > A pointer to a list element which I cannot see irritates me. > > > > I don't understand what you mean. That is, what do mean by see? > For instance, in C, I would have something like int idx; wherein I
could use
> array[idx]; and move around by increasing and decreasing the index marker.
> can I optically see where 'next will put me without evaluating it?
Hmm. C. My first attempt at C, a twenty line program, crashed my machine in spectacular style. I don't really know C, but if my memory serves correctly array[idx] is roughly equivalent to AT array idx in REBOL, except that array[idx] is a memory location and AT array idx is a series! (a "smart value"). AT - Returns the series at the specified index. NEXT is like using AT array 2 If you want to get information out you can use PICK array idx, and to put it in you can use POKE array idx value. So you can use an absolute style of referencing if you want to (presuming array is at its head).
> How can I > find out where REBOL believes itself to be in a series! ? Does REBOL
> it's at the first value, the second, the third, etc? How can I find out?
INDEX? - Returns the index number of the current position in the series.
> Still, I like to know where REBOL is, or thinks it is. I like to be
able to
> have some sort of index instantly available to me like that. Yes, I know,
> can use pick series 3, but when I use 'next and 'skip and whatnot.... I
> lost. And when REBOL modifies the source data when I really only wanted
> look, that irritates me to death, especially when I'm still learning such > manipulations.
No, I doubt that REBOL modifies the source data. Forgive me if I am wrong here but I think you might be referring to this sort of expression: sequence: next sequence The evaluation of this expression should be understood in three steps. I'll try to explain further with examples and use block! for the series!. sequence: [1 2 3 4 5] The line above has two values a set-word! and a block! So somewhere in REBOL's memory a series of values 1, 2, 3, 4, 5 has been created, and is represented by a block! datatype. Subsequently, during evaluation, the word Sequence is set to refer to the new block! (series!) value. But the word is not referring to 1 2 3 4 5, it refers to a block! which has an index and a reference to the sequence of values. Now compare the previous example with this: char-sequence: next [a b c d e] This line has three values - a set-word!, a word! and a block!. When this line is evaluated, again a block! series of values will be created, this time of five words (a, b, c, d, e). The block! will be at its head (head? returns true). Then NEXT is evaluated and it takes the block as input. NEXT returns a different block! value - that is the new block! has a different index to the original but retains a reference to the same sequence of elements. Finally in the evaluation, char-sequence is set to refer to this new block! and if you perform INDEX? on char-sequence afterwards, you will get the value 2 returned. If you performed FIRST HEAD char-sequence you would get the value 'a returned. So now this next expression of six values a: next b: next c: next sequence: [a b c] If you print sequence you will find the original data intact. If you print c you will see "[b c]" this is because the index of the block! that B refers to is equal to 2. If you print b you will see "[c]" this is because the index of the block that A refers to is equal to 3. If you print a you will see "[]" this is because the index of the block that A refers to is equal to 4 and is at the tail of the sequence. Executing REMOVE B will change the underlying data sequence, but each block! that A, B and C refer to do not change. Now if you try printing A you will get an error message. This is because A's index is past the tail of the series. But A can still be used try printing HEAD A. So in this last example I have made four seperate references to the same underlying data series and each of these is referred to by a different word (A, B, C and Sequence). So now, at last :^) I make my point. In effect, when you use NEXT you get a new value returned to you than what you gave it and not something that is modified. But the thing you get back shares the same underlying sequence of values as the input series to NEXT. And when you issue: sequence: next sequence instead of: next-bit: next sequence you are effectively choosing to discard the original reference.
> I'm still going to have to work on this more. I can see very often
> REBOL is easier, particularly for new coders to learn. But it strikes me
> people who've been coding for a while in languages like C run up against
> walls. ;)
This was a significant issue for me. Just prior to learning REBOL a lot of my commercial experience was using a compiled language and I had not met functional languages before. The hardest things for me were to accept the descriptions of REBOL evaluations in the documentation at face value and to stop looking at lines of code (compiled in my mind) and instead look at values and expressions being evaluated one ofter the other.
> Thanks for your help, Brett.
My pleasure. I hope the descriptions above are useful and not uselessly wordy! Brett.