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

What does "loop" return?

 [1/12] from: valeri:mytinski::gmail at: 4-Apr-2009 22:38


Hi! I am novice in REBOL - 4 days learning, although 20 years in programming. My question is: what does "loop" ("foreach" and so on) return? Namely:
>> r: loop 2 [append [] 3]
== [3 3]
>> help r
R is a block of value: [3 3]
>> r: loop 2 [3]
== 3
>> help r
R is an integer of value: 3 In first case "loop" returns block (seems like ...?) while in second it returns integer (seems like last evaluated value). So there are some questions: - is that desirable ? - what is the sense of such behaviour ? - why only second case is described in REBOL/Core doc ? (and for REBOL 3 too) - if I am wrong and in first case "loop" returns value too then how this value (block [3 3]) is calculated ? Thanks in advance. I can't go ahead without understanding base things. Valeri

 [2/12] from: gregg::pointillistic::com at: 4-Apr-2009 13:54


Hi Valeri, Welcome to REBOL! ÂÌ> My question is: what does "loop" ("foreach" and so on) return? Namely: ... ÂÌ> (seems like last evaluated value). Correct. ÂÌ> So there are some questions: ÂÌ> - is that desirable ? What else would you suggest they return? ÂÌ> - what is the sense of such behaviour ? Consistency, and the ability to use the result. ÂÌ> - why only second case is described in REBOL/Core doc ? (and for REBOL ÂÌ> 3 too) I'm not sure what you mean here. ÂÌ> - if I am wrong and in first case "loop" returns value too then ÂÌ> how this value (block [3 3]) is calculated ?
>> loop 2 [append [] 3]
== [3 3]
>> append append [] 3 3
== [3 3]
>> r: append [] 3
== [3]
>> append r 3
== [3 3] In general, think about what REBOL returns when a block is evaluated. What are you doing when you use LOOP, or FUNC? What does a function return (assuming you don't use EXIT or RETURN in the body)? REBOL is great fun, but it is also very different from other languages. Don't be discouraged by things that seem odd, and feel free to post any questions, even if they seem simple. -- Gregg

 [3/12] from: valeri:mytinski:gm:ail at: 5-Apr-2009 8:45


04.04.2009, =D0=B2 23:54, Gregg Irwin =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB(=D0=B0): Thank you, Gregg.
> =C3=82=C3=8C> (seems like last evaluated value). > > Correct. > > =C3=82=C3=8C> So there are some questions: > =C3=82=C3=8C> - is that desirable ? > > What else would you suggest they return?
For example: series of all evaluated values ;-) So loop 2 [append [] 3] == [[3] [3]] and loop 2 [3] == [3 3]
>>> loop 2 [append [] 3] > == [3 3] > >>> append append [] 3 3 > == [3 3] >
Why "append append [] 3 3" not "append [] 3 append [] 3"? I do not understand this point. Valeri=

 [4/12] from: compkarori::gmail::com at: 5-Apr-2009 17:00


append [ ] 3 returns the value [ 3 ] this value is a block containing the single element 3, and it then becomes the parameter for the next 'append hence append append [] 3 3 but if you do append [ ] 3 append [ ] 3 you are performing the append on two different empty blocks. 2009/4/5 =F7=C1=CC=C5=D2=C9=CA =ED=D9=D4=C9=CE=D3=CB=C9=CA <valeri.mytinski-gmail.com>:
> Why "append append [] 3 3" not "append [] 3 append [] 3"? > I do not understand this point.
-- Graham Chiu http://www.synapsedirect.com Synapse - the use from anywhere EMR.

 [5/12] from: valeri:mytinski:gmai:l at: 5-Apr-2009 10:19


05.04.2009, =D7 9:00, Graham Chiu =CE=C1=D0=C9=D3=C1=CC(=C1):
> append [ ] 3 > returns the value [ 3 ] > > this value is a block containing the single element 3, and it then > becomes the parameter for the next 'append >
Yes, I see what is happening. I read code: loop 2 [append [] 3] as "do 2 times block of code [append [] 3]". Apparently this is not right. Could you please suggest some rule for reading? Valeri=

 [6/12] from: compkarori::gmail::com at: 5-Apr-2009 18:34


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 9:00, Graham Chiu =CE=C1=D0=C9=D3=C1=CC(=C1): >>
<<quoted lines omitted: 9>>
> right. > Could you please suggest some rule for reading?
The first time 3 is appended to the empty block. The second time 3 is appended to the now non empty block which contains 3 you end up with [ 3 3 ] If you wish to create a new empty block each time, you would do this instead loop 2 [ append copy [] 3 ] which would be the same as append [ ] 3 append [ ] 3 -- Graham Chiu http://www.synapsedirect.com Synapse - the use from anywhere EMR.

 [7/12] from: valeri:mytinski:gm:ail at: 5-Apr-2009 14:23


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
<<quoted lines omitted: 3>>
> 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? Valeri

 [8/12] 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
<<quoted lines omitted: 21>>
> 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

 [9/12] from: valeri:mytinski::gmail at: 5-Apr-2009 20:17


05.04.2009, =D7 15:42, Henrik Mikael Kristensen =CE=C1=D0=C9=D3=C1=CC(=C1):
> 2009/4/5 =F7=C1=CC=C5=D2=C9=CA =ED=D9=D4=C9=CE=D3=CB=C9=CA > <valeri.mytinski
<<quoted lines omitted: 76>>
> Regards, > Henrik Mikael Kristensen
Thank you very much, Henrik! Valeri PS Do you know more tricks that anyone must know BEFORE start real coding? :-)

 [10/12] from: anton::wilddsl::net::au at: 6-Apr-2009 3:00


Try these, I wrote them to help beginners: (Works in Rebol/View 2, not R3.) do-thru http://anton.wildit.net.au/rebol/demo/demo-series.r do-thru http://anton.wildit.net.au/rebol/doc/rebol-values.r Regards, Anton. =F7=C1=CC=C5=D2=C9=CA =ED=D9=D4=C9=CE=D3=CB=C9=CA wrote:

 [11/12] from: gregg::pointillistic::com at: 5-Apr-2009 11:57


Hi Valeri, ֒> PS Do you know more tricks that anyone must know BEFORE start ֒> real coding? :-) It's hard to say, because everyone's idea of "real" coding is different. You can do a lot without knowing the details. You can definitely start with the Core guide, though some bits are out of date. It doesn't delve into the inner details. http://www.rebol.com/docs.html http://www.rebol.com/tutorials.html http://musiclessonz.com/rebol_tutorial.html If you want something deeper, check out Ladislav's articles, but many of them are *not* beginner material. http://www.fm.tul.cz/~ladislav/rebol/ http://en.wikibooks.org/wiki/REBOL_Programming/Language_Features/Series -- Gregg

 [12/12] from: anton::wilddsl::net::au at: 6-Apr-2009 14:56


Try these, I wrote them to help beginners: (Works in Rebol/View 2, not R3.) do-thru http://anton.wildit.net.au/rebol/demo/demo-series.r do-thru http://anton.wildit.net.au/rebol/doc/rebol-values.r Regards, Anton. =D0=92=D0=B0=D0=BB=D0=B5=D1=80=D0=B8=D0=B9 =D0=9C=D1=8B=D1=82=D0=B8=D0=BD=D1=81=D0=BA=D0=B8=D0=B9 wrote:

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted