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

[REBOL] Trying to compose a block out of global and local bindings Re:(2)

From: lmecir:geocities at: 8-Sep-2000 21:38

Hi Elan, you wrote:
> letter2: func [b] [ > foreach name ["Sue" "Sally"] [ > print bind b 'name > ] > ] > > Here BIND directs REBOL to associate all words contained in 'b with the > "closest" context that contains the symbol name.
L: I would recommend to change the wording: {Bind associates all words contained in B (not in 'B, because in 'B is contained a block IMHO) with the context previously associated with the word 'Name (again, not Name, as you wrote) which is its second argument.} I am not sure I understand the term "closest" above, I would recommend to not use it here. Elan:
> Because > 1. the context in which the BIND expression is being used is the block > passed to foreach, and
L: Again, a change suggested. I don't believe, that the block passed to Foreach is a context, sorry. This wording looks better: {The second argument of Bind is the word 'Name previously associated with the context Foreach created.} Elan:
> 2. > a) because the first argument passed to foreach (foreach name ...) is an > instance of the word name
L: This doesn't make much sense to me too, the sentence: {'Name (the second argument of Bind above) is an instance of the Word! datatype} looks like a correct usage of the word "instance", but it doesn't say what you wanted. I would recommend to look at the previous suggestion. Elan:
> b) there therefore now exists an instance of the word name that is bound
to
> foreach's context,
L: Again a change necessary, IMHO: {There exists a word 'Name that is bound to a context Foreach created.} is the wording I prefer. Elan:
> 3. ==> therefore > a) REBOL will identify foreach's context as the "closest" context in which > 'name occurs.
L: I think, that you switched the cause and the consequence here: the truth is, that the word 'Name above (the second argument of Bind) was bound by Foreach to the context it created and that is why "there exists a word 'Name that is bound to a context Foreach created". Elan:
> This is the context to which all words in the block b are now > being bound, provided they are defined in foreach's context. > b) Because of 3.a) the symbol name in the block b will be bound to the > foreach context. (If the block b contained other words and some or all of > these other words were also defined in the foreach context, then all these > words would be bound to the foreach context as well.
L: I don't like the use of the word "symbol" here. The meaning of it is defined in Rebol. I found the use of this word in Rebol with different meaning, than Rebol assigned to it ambiguous. Elan:
> Words that are not > defined in the foreach context remain bound to whichever context they > originated in, when the block b was formed, or they remain unset!, if they > were never set to a value to begin with).
L: Again a statement I would suggest to change: {Words that are not contained (whether defined or not is irrelevant) in the context Foreach created remain unchanged, while words, that are contained in the context Foreach created are replaced (in B) by their equivalents bound to the above context.} Elan:
> RESULT: > > >> name: "Bob" > == "Bob" > >> message: ["hi" name "welcome back"] > == ["hi" name "welcome back"] > >> letter2: func [b] [ > [ foreach name ["Sue" "Sally"] [ > [ print bind b 'name > [ ] > [ ] > >> letter2 message > hi Sue welcome back > hi Sally welcome back > > Note that as a side-effect the symbol name in the message block remains > bound to the value it was last associated with in the letter2 function: > > >> reduce message > == ["hi" "Sally" "welcome back"] >
L: Yes, this effect can be considered a self-modification of the code presented, I think. Elan:
> If that should be relevant, i.e. you want to prevent the modification of > the binding of the word name in the original message block, you can use > bind/copy, which generates a duplicate of the block, before it binds it: > > letter2: func [b] [ > foreach name ["Sue" "Sally"] [ > print bind/copy b 'name > ] > ] > > >> letter2 message > hi Sue welcome back > hi Sally welcome back > >> reduce message > == ["hi" "Bob" "welcome back"] > > Note that the word used as foreach's first argument (foreach name ...) is > not bound in the context of your letter2 function in which foreach is > evaluated. It is bound in the context of foreach.
L: A different wording: {Note that the word used as Foreach's first argument (foreach name ...) is not bound in the context of your Letter2 function. It is bound in the context Foreach created.} I would prefer not saying, that "...Foreach is evaluated in a context,...", because I don't know, what that should mean. To the contrary, I am pretty sure, that there are examples of Rebol code, for which you cannot find any context they are evaluated in . Elan:
> Let us demonstrate that by collecting different instances of name into a > block. We being with the global instance: > > >> names: [] > == [] > >> name: "This is the global instance of name." > == "This is the global instance of name." > >> insert tail names 'name > == [] > >> names > == [name] > >> reduce names > == ["This is the global instance of name."] > > Now a function that has a local instance of name > f: func [/local name] [ > name: "This is the function's local instance of name." > insert tail names 'name > print "In f. Before foreach." > print mold names > print names > > and includes a foreach loop that also uses an instance of the word (or > symbol) name: > > foreach name ["This is the foreach instance of name."] [ > insert tail names 'name > print "In foreach." > print mold names > print names > ] > > Now the complete f function including both instances of name: > > f: func [/local name] [ > name: "This is the function's local instance of name." > insert tail names 'name > print "In f. Before foreach." > print mold names > print names > foreach name ["This is the foreach instance of name."] [ > insert tail names 'name > print "In foreach." > print mold names > print names > ] > print "After foreach. In f." > print mold names > print names > ] > > ... and let's run it: > > >> f > In f. Before foreach. > [name name] > This is the global instance of name. This is the function's local instance > of name. > In foreach. > [name name name] > This is the global instance of name. This is the function's local instance > of name. This is the foreach instance of name. > After foreach. In f. > [name name name] > This is the global instance of name. This is the function's local instance > of name. This is the foreach instance of name. > > Note that because we bound foreach's instance of name to the block's > context, and the block is defined globally, the context foreach's name has > been extended, i.e. it remains "alive". > > Hope this helps a little. > > Take Care, > > At 01:02 PM 9/7/00 -0700, you wrote: > >>> letter2: func [b /local name] [foreach n ["sally" "sue"][ name: n
print
> reform reduce b] ] > > > >>> form > >== ["hi" name "welcome back"] > > > >>> name > >== "bob" > > > >>> letter2 form > >hi bob welcome back > >hi bob welcome back > > > >... the only problem is I was hoping that the loop values in letter2
would
> take precedence over the globally bound value of name and allow me to > create a form letter of sorts. > > > >Could anyone help with this please? > > > > > > > >Get your FREE Email and Voicemail at Lycos Communications at > >http://comm.lycos.com > > > > > > > > ;- Elan [ : - ) ] > author of REBOL: THE OFFICIAL GUIDE > REBOL Press: The Official Source for REBOL Books > http://www.REBOLpress.com > visit me at http://www.TechScribe.com >
Take Care, Ladislav