[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Newbie question: bind a variable on the fly
From: |
Jonathan Wilson |
Subject: |
Re: Newbie question: bind a variable on the fly |
Date: |
Tue, 13 Jun 2006 13:26:42 -0500 |
User-agent: |
Thunderbird 1.5.0.4 (X11/20060516) |
Hi Neil,
Neil Jerram wrote:
Jon Wilson <address@hidden> writes:
(define-macro (dyn-set! var val)
`(begin (if (not (defined? (quote ,var)))
(primitive-eval `(define ,(quote ,var) #f)))
(set! ,var ,val)))
(defined? 'undefined-symbol) ; => #f
;(set! undefined-symbol #t) Gives an error.
(dyn-set! undefined-symbol #t) ; No error.
(defined? 'undefined-symbol) ; => #t
- isn't ,(quote xxx) equivalent to just xxx?
You'd think so. But it didn't wind up working out that way. Try it and
see. Perhaps this is a bug? I suspect not, however, what with having
the nested quasiquotes, with one of them inside the primitive-eval.
I'm afraid I don't 100% understand just what I wrote... With the nested
quasiquotes here, just what gets expanded when? I guess that
(dyn-set! yyz 5)
becomes (under the macro expansion)
(begin (if (not (defined? 'yyz)))
(primitive-eval `(define ,(quote yyz) #f)))
(set! yyz 5)))
And then the other quasiquote gets expanded to
(define yyz #f)
But, I'm not quite sure why this should work this way. It seems like
the first expansion should be
(begin (if (not (defined? 'yyz)))
(primitive-eval `(define ,var #f)))
(set! yyz 5)))
But in this expression, is var still bound to yyz? It seems like this
expansion shouldn't work, we should get a "symbol var is undefined" sort
of error. But we don't.
- as you kind of said, it will go wrong if ,var is defined locally:
defined? will be #f for a local variable, so a top-level variable
with the same name will be created, but the set! will then set the
local variable.
Ah yes, that is an important twist I hadn't thought of. Is there a
`local-defined?' function anywhere?
Also, I found myself completely unable to bind this macro to the symbol
set!, even by strange things like defining real-set! to be set!, and
then defining the macro, and then (real-set! set! dyn-set!). Bad things
happened to my poor abused guile.
Thanks for taking a look!
Regards,
Jon