This is the mail archive of the
mailing list for the Guile project.
Re: need SETQ
On Fri, Feb 04, 2000 at 03:50:18PM -0800, Michael Vanier wrote:
> On a related note, can someone post a macro that binds a value to an
> identifier without requiring that the identifier be define'd first? I've
> often been puzzled by why scheme requires the programmer to define a
> variable before assigning to it, in contrast to lisp. Of course, you can
> (and generally do) assign a value when you define the variable, but
> subsequent assigns must use set! instead of define (unless you want to
> redefine the variable each time you use it, which seems inefficient).
> I'm looking for something like set! which first checks to see if the
> variable has been defined. If not, it defines it and binds the value; if
> so, it just binds the value. This is especially useful within loops.
I couldn't find a way to write such a macro, because in Scheme there are
restrictions on where you can use `define' which doesn't apply to `set!'.
It doesn't seem posible to use `let' instead of `define', because you have
to close it in the macro.
But generally I don't think it's a good feature to have and use because I
and VBScript (in the latter one can turn it off, though). In PHP3 it's a
little bit better, because one creates a local variable instead of global.
> This is also relevant to translators for other scripting languages, since I
> know of none that don't allow the user to assign a variable to a new value
> with the same syntax as for the initial binding of a variable to a value.
When you write a translator, you can check at translate time if this
variable is defined or not and use the appropriate construction.
> I'd be interested in hearing the philosophy behind the scheme choice. I
> have no religious bias on the issue :-)
> P.S. Looking at the macros that were posted for the set! that returns a
> value, I have to say that the define-syntax macros are exceptionally
> elegant and easy to read. I wish this style of macros were used more
Me too. Though the LISP style macros are more powerful.