hello,
consider this :
(let ((first-name nil)
(sir-name nil))
(defun set-variable (variable new-value)
(setf variable new-value)))
(set-variable first-name "George")
(set-variable sir-name "Tasso")
i'd like to write a general function within a lexical scope assign a
value to any variable that is define in the lexical scope, like above.
i don't want to write two function one for assigning first-name and
one for sir-name
can someone show me how to make this happen ?
many thanks.
George
·······@gmail.com wrote:
> consider this :
>
> (let ((first-name nil)
> (sir-name nil))
> (defun set-variable (variable new-value)
> (setf variable new-value)))
>
> (set-variable first-name "George")
> (set-variable sir-name "Tasso")
>
>
> i'd like to write a general function within a lexical scope assign a
> value to any variable that is define in the lexical scope, like above.
> i don't want to write two function one for assigning first-name and
> one for sir-name
>
> can someone show me how to make this happen ?
Once things compile, the lexical names are optimized away (well, they
can be; assume the worst).
During compilation, you need to latch on to them somehow so they exist
at runtime. Options include hashtables, arrays, and other data
structures.
Here's a direct implementation of what you asked for
(let ((first-name nil)
(middle-name nil)
(last-name nil))
(defun set-name (name &optional (part :first))
(ecase part
(:first (setf first-name name))
(:middle (setf middle-name name))
(:last (setf last-name name))))
(defun get-name ()
(list first-name middle-name last-name)))
But you'll probably be happier with a simple
(defstruct full-name
(first nil)
(middle nil)
(last nil))
- Daniel