[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: |
Neil Jerram |
Subject: |
Re: Newbie question: bind a variable on the fly |
Date: |
Mon, 12 Jun 2006 23:17:48 +0100 |
User-agent: |
Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux) |
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
I think this mostly does what you want, but
- isn't ,(quote xxx) equivalent to just xxx?
- 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.
Regards,
Neil