From: dave yee
Subject: query 'bout defstruct (Common LISP)
Date: 
Message-ID: <10834@cgl.ucsf.EDU>
Hi there,

i have an easy question for you LISP experts...
First of all, i'm using common LISP.


i would like to define a data structure using defstruct.  
For example:
  (defstruct (piece)
	     (is-viable t)
	     (coord-x   0)
	     (coord-y   0)
	     (coord-z   0))

so far, so good.  Now for the tricky part.  I will have a variable
number of "pieces" at any given time in the program.
So i would like the computer to
generate new symbols for me.  The new symbols would then
be used as names for the new pieces. 

Now, i figured i could use "gentemp" to make the symbols,
but i can't "setq" these new symbols (i.e.
(setq (gentemp) (make-piece)) just doesn't cut it...

Anyone have any suggestions?  Is there anotyher way i can
have the program create new symbols which could be
used in this way?

any suggestions would be appreciated...
-- 
-dave: 
      ARPA:   ···@cgl.ucsf.edu         "Language is a 
           UUCP:   ucbvax!ucsfcgl!yee          Virus from
                Bitnet: ···@ucsfcgl.BITNET          Outer Space"

From: john nienart
Subject: Re: query 'bout defstruct (Common LISP)
Date: 
Message-ID: <786@hydra.riacs.edu>
In article <·····@cgl.ucsf.EDU> ···@cgl.ucsf.edu (dave yee) writes:
>
>Now, i figured i could use "gentemp" to make the symbols,
>but i can't "setq" these new symbols (i.e.
>(setq (gentemp) (make-piece)) just doesn't cut it...
>
How about (setf (symbol-value (gentemp)) (make-piece))? This gets the
effect you want. However, you don't have a handle on the name after
this, so you probably want something like:

(let ((sym (gentemp)))
  (setf (symbol-value sym) (make-piece))
  ...)
which gives SYM a value which is the name of your piece. Symbol-value can 
access it then.

Hope this helps.

--John
·······@turing.arc.nasa.gov
From: Eliot Handelman
Subject: Re: query 'bout defstruct (Common LISP)
Date: 
Message-ID: <2408@mind.UUCP>
In article <···@hydra.riacs.edu] ·······@turing.arc.nasa.gov.UUCP (john nienart) writes:
]In article <·····@cgl.ucsf.EDU> ···@cgl.ucsf.edu (dave yee) writes:
]>
]>Now, i figured i could use "gentemp" to make the symbols,
]>but i can't "setq" these new symbols (i.e.
]>(setq (gentemp) (make-piece)) just doesn't cut it...
]>
]How about (setf (symbol-value (gentemp)) (make-piece))? This gets the
]effect you want. However, you don't have a handle on the name after
]this, so you probably want something like:
]
](let ((sym (gentemp)))
]  (setf (symbol-value sym) (make-piece))
]  ...)
]which gives SYM a value which is the name of your piece. Symbol-value can 
]access it then.


Why not just use (set (gentemp) (make-piece))? It does the same thing without
bothering to hunt down the setf method. If you need the handle you can either
use LET or have the new name returned as a second value:

(defun set-gentemp (form)
  (let ((sym (gentemp)))
   (values (set sym form) sym)))
 
Best wishes,

Eliot Handelman
From: ······@uicslsv.cs.uiuc.edu
Subject: Re: query 'bout defstruct (Common LISP)
Date: 
Message-ID: <4400004@uicslsv>
There might be simpler ways but I use a macro for a similar purpose.

How 'bout ..

(defmacro junk (&aux gen-var)
   (setf gen-var (gentemp "FOO-"))
   `(setf ,gen-var ,(make-piece)))
  ^^^ Thats a back-quote.

It is also trivial to push the generated symbols onto some global list
to keep track of them if necessary.

-Bharat

············@a.cs.uiuc.edu
From: Rick Wojcik
Subject: Re: query 'bout defstruct (Common LISP)
Date: 
Message-ID: <5183@bcsaic.UUCP>
In article <·····@cgl.ucsf.EDU> ···@cgl.ucsf.edu (dave yee) writes:
>
>Now, i figured i could use "gentemp" to make the symbols,
>but i can't "setq" these new symbols (i.e.
>(setq (gentemp) (make-piece)) just doesn't cut it...
>
Try (set (gentemp) (make-piece)).  
-- 
Rick Wojcik   csnet:  ·······@boeing.com	   
              uucp:   uw-beaver!ssc-vax!bcsaic!rwojcik 
address:  P.O. Box 24346, MS 7L-64, Seattle, WA 98124-0346
phone:    206-865-3844
From: Larry Baum
Subject: Re: query 'bout defstruct (Common LISP)
Date: 
Message-ID: <5190@bcsaic.UUCP>
In article <·····@cgl.ucsf.EDU> ···@cgl.ucsf.edu (dave yee) writes:
>i would like to define a data structure using defstruct.  
>
>...  Now for the tricky part.  I will have a variable
>number of "pieces" at any given time in the program.
>So i would like the computer to
>generate new symbols for me.  The new symbols would then
>be used as names for the new pieces. 
>
>Now, i figured i could use "gentemp" to make the symbols,
>but i can't "setq" these new symbols (i.e.
>(setq (gentemp) (make-piece)) just doesn't cut it...
>

You could do: (set (gentemp) (make-piece))

However, while this will successfully do the binding, it will defeat
your goal of having a name for your object, since the symbol returned
by (gentemp) has been lost.  Better would be:

(let ((symb (gentemp)))
   (set symb (make-piece))
   ... code to get a handle on symb ...)

If you are going to have many, many pieces, then storing them in a hash
table will probably give to better performance.

LSB
From: Bruce Krulwich
Subject: Re: query 'bout defstruct (Common LISP)
Date: 
Message-ID: <28800@yale-celray.yale.UUCP>
In article <·····@cgl.ucsf.EDU> ···@cgl.ucsf.edu (dave yee) writes:
>...  Now for the tricky part.  I will have a variable number of
>"pieces" at any given time in the program.  So i would like the
>computer to generate new symbols for me.  The new symbols would then
>be used as names for the new pieces.

I have to ask:  Why would you ever want to do this??  If you're going
to have a variable number of pieces then you'll want to store them in
a list or table anyway.  Why bother GENing symbols??  

In article <····@bcsaic.UUCP> ·····@bcsaic.UUCP (Larry Baum) writes:
>However, while this will successfully do the binding, it will defeat
>your goal of having a name for your object, since the symbol returned
>by (gentemp) has been lost.  Better would be:
>
>(let ((symb (gentemp)))
>   (set symb (make-piece))
>   ... code to get a handle on symb ...)

Why??  What does this buy you over simply

	(let ((piece (make-piece))) ...)

The only reason I can see this being a way to do things is if you're
making piece structures at macro-expand time, which seems gross.  Is
there another reason??

Bruce Krulwich
From: Alex S. Crain
Subject: Re: query 'bout defstruct (Common LISP)
Date: 
Message-ID: <974@umbc3.UMD.EDU>
In article <·····@yale-celray.yale.UUCP> ··············@CS.YALE.EDU (Bruce Krulwich) writes:
>In article <·····@cgl.ucsf.EDU> ···@cgl.ucsf.edu (dave yee) writes:
>>...  Now for the tricky part.  I will have a variable number of
>>"pieces" at any given time in the program.  So i would like the
>>computer to generate new symbols for me.  The new symbols would then
>>be used as names for the new pieces.
>
>I have to ask:  Why would you ever want to do this??  If you're going
>to have a variable number of pieces then you'll want to store them in
>a list or table anyway.  Why bother GENing symbols??  

	I did not post the original request, but I have this identical problem
for a project that I'm doing now. The motivation behind the gensyms is to 
avoid a list or table structure. My project is projecting a dynamic knowege
base over a neural net built on interrelated symbols. Symbols have the 
advantage of not having an explicit value when they are generated, so I can
bind an undefined object, and define it's value later.

	The symbol tags also mean that I don't have to jerk around with a table
lookup,	in that I can use the existing interface (ie: the obstack).

	Using symbols over list elements or table indexes makes tracing
through the resulting data structure a piece of cake.

	Symbols can have plists.


	I welcome rebuttles to any of these motives....

-- 
					:alex.

···········@umbc3.umd.edu
····@umbc3.umd.edu
From: John Hanley
Subject: Re: query 'bout defstruct (Common LISP)
Date: 
Message-ID: <384@mancol.UUCP>
>The motivation behind the gensyms is to avoid a list or table structure.

I submit that you probably don't have a clear enough idea of what you want
your data structure to look like if you're using arguments like this.
Picking an appropriate data structure is crucial to coming up with a good
algorithm, and can structure the way you view the problem.  It's worth
investing the time in doing some serious thinking about your data structure,
and examining and discarding alternatives.

>Symbols have the advantage of not having an explicit value when they are generated
Is 'this-is-not-a-value a bad value to give to a brand new table entry?

>The symbol tags also mean that I don't have to jerk around with a table
>lookup, in that I can use the existing interface (ie: the obstack).

In Vax Lisp, symbol->value lookups are done with a big hashtable, named 'user
(or whatever your favorite package name is).  I don't know about other Common
Lisps, but I assume that as long as they go to the trouble of implementing
generic hash tables, they might as well use them for symbol lookups.

Y'all are familiar with (gethash), right?  If not, you are strongly urged to
look it up in Guy Steele's "Common Lisp: the language" or to at least type
(describe 'gethash).  Hash tables are great:  the functionality of assoc lists
(well, OK, so you can't go backwards) plus the efficiency of hashing, and you
never have to worry about performance while you're just testing because tables
automatically grow to accomodate more items. 


>	Symbols can have plists.

Ugh!  Now you really don't know what you're doing!  Plists are to data
structures as goto's are to control structures.  1/2-)
Seriously, they have their uses sometimes, but there's no reason the
same information couldn't be stored as a table (so now we have a table
of values, and each value is itself a table).  I don't see anything
special about symbols that makes them inherently more powerful than
a-lists or hashtables.  If you like symbols, fine, use them.  I kind
of like the ability to print out my table at any time and see the
_entire_ data structure so I can see if things are as they should be.


             --John Hanley
              System Programmer, Manhattan College
              ..!cmcl2.nyu.edu!manhat!jh  or  ······@nyu.edu   (CMCL2<=>NYU.EDU)



In case the original article expired on your system, here's the discussion
I was adding to:

In article <···@umbc3.UMD.EDU> ····@umbc3.UMD.EDU (Alex S. Crain) writes:
>In article <·····@yale-celray.yale.UUCP> ··············@CS.YALE.EDU (Bruce Krulwich) writes:
>>In article <·····@cgl.ucsf.EDU> ···@cgl.ucsf.edu (dave yee) writes:
>>>...  Now for the tricky part.  I will have a variable number of
>>>"pieces" at any given time in the program.  So i would like the
>>>computer to generate new symbols for me.  The new symbols would then
>>>be used as names for the new pieces.
>>
>>I have to ask:  Why would you ever want to do this??  If you're going
>>to have a variable number of pieces then you'll want to store them in
>>a list or table anyway.  Why bother GENing symbols??  
>
>	I did not post the original request, but I have this identical problem
>for a project that I'm doing now. The motivation behind the gensyms is to 
>avoid a list or table structure. My project is projecting a dynamic knowege
>base over a neural net built on interrelated symbols. Symbols have the 
>advantage of not having an explicit value when they are generated, so I can
>bind an undefined object, and define it's value later.
>
>	The symbol tags also mean that I don't have to jerk around with a table
>lookup,	in that I can use the existing interface (ie: the obstack).
>
>	Using symbols over list elements or table indexes makes tracing
>through the resulting data structure a piece of cake.
>
>	Symbols can have plists.
>
>
>	I welcome rebuttles to any of these motives....
>
>-- 
>					:alex.
>
>···········@umbc3.umd.edu
>····@umbc3.umd.edu
From: Alex S. Crain
Subject: Re: query 'bout defstruct (Common LISP)
Date: 
Message-ID: <982@umbc3.UMD.EDU>
In article <···@mancol.UUCP> ··@manhat.UUCP (John Hanley) writes:
>>The motivation behind the gensyms is to avoid a list or table structure.
>
>I submit that you probably don't have a clear enough idea of what you want
>your data structure to look like if you're using arguments like this.

	Well, depends on what "enough" is. The truth is, I'm not convinced that
the final version of this project will look much like it does now, and I want
alot of freedom to play with what I have, structure-wise. And that was my 
point.

	Symbols are certainly not an effecient way to build a data structure,
but they do have certain advantages, and (gensym) certainly has its purpose.
As this project progresses, I'm dealing with a progressively complex data
structure where each node may have a several links or more, built as an
n-ary tree structure with interconnected branches. Effeciencey and ellegance
would have me hashing structures as you suggest, but practicality has me 
using symbols because...

	The data structure is really several several structures overlayed
over one another. plists are gross, but they are flexable and fast in an 
interactive environment, which is where I play with my data. I can add or
delete a piece of information from a node without realoading the system, which
I would have to do if I was using structures. For example: My project 
a system for knowelege representation in an natural language environment. One
problem that I will eventaully have to deal with is how word classes affect
parsing action. But this isn't what the project is about (for now) so I wrote
a stupid parser and stuck the strings to the side of the symbols with scotch
tape (plist). When I had to rewrite the parser to take advantage of other
developments, I wrote another stupid parser, and moved the words around and
forgot about it.

	The other big plus was that the project is moving in a cycle that 
goes (1) decide what the data should look like (2) write a program to make the
data look like that (3) make the program work (4) play with the data and decide
what it should really look like (5) go to 2.

	This is fine until the data structure gets so mangled that it can't
be printed sensably, and then there is a problem with checking the output
of the program. symbols provide convienent tags into the data structure so that
I can test by predicting what the value of a symbol shold look like, and then
examining the symbol.

	And yes, I could build a hash table and that would work more 
effeciently (I do know how to use (make-hash-table)), but why? This program,
like alot of what I've been doing lately, will never go beyond the research
stage; ie: it only needs to work. Building what amounts to a customized symbol
table gains me nothing except effeciency that I don't need and another system
to maintain. Besides, If it ever did become desireable, moving to a more 
effecient handeling of graph nodes later would not be difficult, and would
almost certainly be simpler then building it now and fixing it every time
it breaks because of another change.

	My point was (and is) that symbols have ther purpose in life, just
like hash tables and structures, and by writing them off as gross and 
ineffecient, you cut off your nose to spite your face.

	Sort of like goto's :-).


>
>             --John Hanley
>              System Programmer, Manhattan College
>              ..!cmcl2.nyu.edu!manhat!jh  or  ······@nyu.edu   (CMCL2<=>NYU.EDU)
-- 
					:alex.

···········@umbc3.umd.edu
····@umbc3.umd.edu
From: Piet van Oostrum
Subject: Re: query 'bout defstruct (Common LISP)
Date: 
Message-ID: <441@ruuinf.UUCP>
Say you have a symbol piecename where you remember the generated symbol:
	(setq piecename (gentemp))
The you can assign a value to the new symbol with
	(set piecename value)
set assigns only *special* variables, but a lexical variable wouldn't do
any good, because the symbol isn't in your program text anyway.
-- 
Piet van Oostrum, Dept of Computer Science, University of Utrecht
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
Telephone: +31-30-531806              UUCP: ...!mcvax!ruuinf!piet