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

[REBOL] Re: Newest Dillema.

From: joel::neely::fedex::com at: 6-Mar-2003 17:38

Hi, Ed, Let me suggest a different model that might help... Ed Dana wrote:
> >>>test: [ a: "b" "Test me" ] > >>>foreach item test [print mold :item] > >>> > >>> > >a: > >"b" > >"Test me" > > What's the significance of the placement of the colon? I always > understood it to be a type of pointer operation, placing the > colon before meant "give me the contents of" rather than "give > me the value of". > > So, how does placing the colon before make it possible for me > to get the set-word out of the block with out an error? >
Think first of the more obvious case of a function. onemore: func [x [number!]] [x + 1] Now that ONEMORE is set to a FUNCTION! value, we might want to do two distinct things: - GET the function without evaluating it (e.g. to pass it as an argument to another function), or - APPLY the function itself to some argument. The design of REBOL makes both things possible (functions are first- class values), but the notation has to allow us to indicate which one we mean. The REBOL notation assumes that we're more often wanting to do the second (apply) than the first (get without applying). When the word ONEMORE appears (undecorated by any punctuation) in an expression, REBOL will get its associated value, see that it is a FUNCTION! value, and immediately try to apply that function to the next value in the expression. So, in the expression print onemore 3 + 2 the function value is applied to the following value (5) and we get 6 printed on the console. Sometimes we really want to just get the function without (yet) applying it, as in the following higher-order function adjust7: func [f [any-function!]] [f 7] which takes any function as an argument and applies it to seven. In that case, we must explicitly say that we don't want the value of the word to be evaluated/applied (yet). Using a GET-WORD! instead of a WORD! is the way we mention the function without using it. Therefore this fails
>> adjust7 square-root
** Script Error: square-root expected value argument of type: number ** Where: halt-view ** Near: adjust7 square-root because REBOL is trying to APPLY the function, but this succeeds
>> adjust7 :square-root
== 2.64575131106459 because we've asked to fetch it (and pass the unevaluated function to ADJUST7) instead. Now apply this to SET-WORD! values, which also assume that you want to take the action whenever they occur. Here's a transcript that shows that behavior:
>> a: b: 0
== 0
>> print [a b]
0 0
>> stuff-to-set: [a: b:]
== [a: b:]
>> foreach set-this stuff-to-set [set-this 17]
== 17
>> print [a b]
17 17 Inside this FOREACH loop, each use of SET-THIS evaluates to a SET-WORD! which tries to do its thing immediately (just as functions do). If we want to suppress that behavior, we must GET but not EVALUATE the uses of SET-THIS instead:
>> foreach set-this stuff-to-set [print [type? :set-this :set-this]]
set-word a set-word b This way of looking at it (for me at least) seems to help keep things straight without thinking about concepts (e.g. "pointers") that aren't really part of REBOL. HTH! -jn- -- ---------------------------------------------------------------------- Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446 Counting lines of code is to software development as counting bricks is to urban development.