From: Barry Margolin
Subject: Re: Dynamic variable creation in Lisp
Date: 
Message-ID: <5sb10u$pup@tools.bbnplanet.com>
In article <·············@worldnet.att.net>,
Kirt Undercoffer  <·····@worldnet.att.net> wrote:
>I have a question about dynamic variable creation in Lisp.  I've written
>a parser that generates numerous heterogeneous data structures.  I'm
>binding most of these to symbols generated using gentemp since these
>structures will be manipulated by other parts of the program I've
>written.  So the symbol generated is used as a pointer to the individual
>data structure. This in fact was working quite well ...
>
>Anyway, *FEW* of the Lisp references I have actually address dynamic
>variable creation except for symbols created for short term puropses
>using gensym.  So perhaps creating numerous symbols dynamically is anti
>Lisp philosophy?  Additionally, am I correct that the only ways

Dynamic variable creation is usually not appropriate -- it's up there with
EVAL as a hint that you may have a design problem.  Variables are part of
program structure, and shouldn't be used as a general purpose data
structure by themselves.  Rather than passing around a symbol and calling
SYMBOL-VALUE on it, just pass around the data structure itself.  If it's
not a structured data type, but you need a place that you can update, wrap
it in a CONS, DEFSTRUCT, or CLOS object and pass that around.  If you need
to look something up by name, use a hash table.

This last case was a way in which dynamically-created symbols were
frequently used in earlier Lisps.  For instance, language parsing programs
would intern each word and attach attributes using the property list.
Nowadays you would use a hash table for this, rather than interning
symbols.

If you're concerned about the overhead of DEFSTRUCT, don't be.  A symbol is
effectively a structure with four slots: name, value, plist, package.  A
structure with just one slot should use *less* space, and the code to
access a symbol value should be about the same as the code to access a
structure slot.

>available to generate a symbol dynamically are gensym, gentemp, and
>make-symbol?

Another function that creates symbols dynamically is INTERN.

-- 
Barry Margolin, ······@bbnplanet.com
BBN Corporation, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.