[comp.lang.lisp] Basic type conversion??

page@cod.NOSC.MIL (Ward C. Page) (12/22/90)

I have lists of symbols that I would like to use as pointers to structures.
I would like to be able to take the symbol and use it to index the
structure of the same name.  How is this done?  For Example:

	(mapcar 'some-function some-list-of-symbols)

	(defun some-function (symb)
	    (setf symb 'name 'something))

Any way to do this?


Ward Page
Naval Ocean Systems Center
page@cod.nosc.mil

eliot@phoenix.Princeton.EDU (Eliot Handelman) (12/22/90)

In article <2607@cod.NOSC.MIL> page@cod.nosc.mil.UUCP (Ward C. Page) writes:
;I have lists of symbols that I would like to use as pointers to structures.
;I would like to be able to take the symbol and use it to index the
;structure of the same name.  How is this done?  For Example:
;
;	(mapcar 'some-function some-list-of-symbols)
;
;	(defun some-function (symb)
;	    (setf symb 'name 'something))
;
;Any way to do this?


Here's one way.

(defun set-symbols (syms vals)
  (map 'nil #'(lambda (x y) (setf (symbol-value x) y)) syms vals))
		    
So for example (set-symbols '(a b c) '(1 2 3)) sets a to 1, b to 2, etc.

It's not clear what you planned with (setf symb 'name 'something).
Maybe (setf (get symb 'name) 'something), which sets the property
NAME of symb to SOMETHING?  But why would you want to have the symbol's
name as a property? Maybe you wanted to set the structure's NAME
property to something? But only symbols have plists. If, however,
you do want to reference the name of a structure, the easiest way
is to define a NAME slot in the structure itself, like this:

(defstruct MY-DAY
  name
  val-1)

And so to set the name, you write

(setf (my-structure-name a-structure 'clint))

where a-structure is a structure of type MY-DAY.

But lets's say you're using a bunch of different structures, because I
have too much time on my hands and I can't think of anything better to do 
right now. You could define a generic structure, like this:

(defstruct generic-structure
  name
  reasons-for-not-liking-this-structure)

and then include this structure in all other structures, like this:


(defstruct (structure-of-scientific-revolutions (:include generic-structure))
  years-to-revolution
  paradigm-shift
  other-things-that-one-could-be-doing)

And then, if *a* is a structure of this sort, you can then write:

(setf (structure-of-scientific-revolutions-name *a*) 'kuhn),

and so on for all other structures.


Now let's say that you can't do this, and you still need to get at 
the name of a structure from the structure itself. You could use
hash-tables! Here's how it's done. First, define a hash-table:

(defvar *structure-dossier* (make-hash-table))

then, when you make a structure, say like this:

(defvar *a-structure* (make-my-day))

you insert it into the hash-table, like this:

(setf (gethash *a-structure* *structure-dossier*) 'danforth)

And then you can read the name of a structure by saying:

(gethash *a-structure* *structure-dossier*).

Hope that helps.