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

[REBOL] Re: What does "loop" return?

From: henrikmk::gmail::com at: 5-Apr-2009 13:42

2009/4/5 =F7=C1=CC=C5=D2=C9=CA =ED=D9=D4=C9=CE=D3=CB=C9=CA <valeri.mytinski-gmail.com>:
> > 05.04.2009, =D7 10:34, Graham Chiu =CE=C1=D0=C9=D3=C1=CC(=C1): > >> >> If you wish to create a new empty block each time, you would do this ==
20
>> instea >> d >> >> loop 2 [ append copy [] 3 ] >> >> which would be the same as >> >> append [ ] 3 append [ ] 3 >> >> Graham Chiu > > Thanks for this example. > Sorry, now I understood nothing. > > Why > > loop 2 [append [] 3] > > is equivalent to > > append append [] 3 3 > > that is some sort of "nested" expressions - result of first 'append is > INSERTED as parameter for second 'append, > > while > > loop 2 [ append copy [] 3 ] > > is equivalent to > > append [ ] 3 append [ ] 3 > > that is some sort of "sequence" expressions - first 'append is > FOLLOWED by second 'append? Why does not parameter substitution > done?
You are entering a deep issue in REBOL, namely that certain values are aggressively reused and that REBOL works by copying as little data as possible by default to let it be very efficient. That might be a bit hard to understand at this point. Let's look at the loop: loop 2 [append [] 3] And strip the stuff in front of the block: [append [] 3] When you specify the loop like this, REBOL immediately recognizes the [] you specify as being literally the same memory position that APPEND should work on, each time it runs through the loop. This is very important to understand. In a sense the loop becomes self modifying. This is a neat trick that lets you avoid referencing the block from a word, but it can also be a nasty trap, if you don't realize this. So when you try this one: append [] 3 append [] 3 You have two different blocks at two different memory locations. When you do a COPY, the block you work on is never the same, but always a copy. This trick works everywhere, in blocks, loops, functions, etc. But be careful when you use it. REBOL has a function for testing the SAMEness of series/blocks, i.e. whether two series you are testing on, is the same one at the same memory location. It's called SAME?. -- Regards, Henrik Mikael Kristensen