From: ·······@gmail.com
Subject: access outside lexical scope
Date: 
Message-ID: <d34c0036-634b-41d0-a853-9e37d479199a@d4g2000prg.googlegroups.com>
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
From: D Herring
Subject: Re: access outside lexical scope
Date: 
Message-ID: <q-ednW1mAIWEsaDanZ2dnUVZ_qOknZ2d@comcast.com>
·······@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