I just have a few more questions on the subject.
First, suppose I have a structure such that:
(defstruct car color year)
And I create some car structures. Is there any way to have a newly
created symbol refer to one of these attributes of a specific car? For
example, suppose I want to redefine the color of a car. I would use
(setf (car-color somecar) blue).
Is there any way to have a symbol refer to (car-color somecar), so
that I can use (setf *somesymbol* blue), and it would update the value
of the car. That is, it wouldn't just refer to the value of the car
when set, but it would refer to (car-color somecar). I have other
examples, but structures seemed most easy to convey what I am asking.
Is there any simplified way to export all the structure functions from
a package. When I use (defstruct car color year), it comes with a
whole bunch of functions for easily modifying the structure. Is there
any way that all these can be easily exported from the package that
the structure was defined in?
Lastly, how can symbols, or the elements they refer to, be deleted?
Basically, if I used (defvar x 10), is there any way to basically undo
this, as if it had never been set?
Thanks.
dstein64 wrote:
> Is there any way to have a symbol refer to (car-color somecar), so
> that I can use (setf *somesymbol* blue), and it would update the value
> of the car. That is, it wouldn't just refer to the value of the car
> when set, but it would refer to (car-color somecar). I have other
> examples, but structures seemed most easy to convey what I am asking.
SYMBOL-MACROLET could emulate that to a degree, but the better way
would probably be not needing that functionality to begin with.
dstein64 wrote:
> Is there any simplified way to export all the structure functions from
> a package. When I use (defstruct car color year), it comes with a
> whole bunch of functions for easily modifying the structure. Is there
> any way that all these can be easily exported from the package that
> the structure was defined in?
0) CAR is a really bad structure name in lisp.
I don't think so[1]. But you could use DO-SYMBOLS to search for names
or another function to iterate over them. Then use (EXPORT
(FIND-SYMBOL name))...
e.g.
(defun map-prefixed-symbols (struct &optional (fn #'print))
(let* ((prefix (symbol-name struct))
(l0 (length prefix)))
(do-symbols (s)
(let* ((name (symbol-name s))
(l (length name)))
(when (and (> l l0)
(string= prefix name :end2 4))
(funcall fn name))))))
(defun list-prefix-symbols (prefix suffixes)
(let ((p (symbol-name prefix)))
(loop for s in suffixes
collecting (concatenate 'string p (symbol-name s)))))
CL-USER> (defstruct car color model)
CL-USER> (map-prefixed-symbols 'car-)
"CAR-MODEL"
"CAR-P"
"CAR-COLOR"
CL-USER> (list-prefix-symbols 'car- '(model p color))
("CAR-MODEL" "CAR-P" "CAR-COLOR")
- Daniel
[1] The man keeps oppressing me; he calls me a lisp noob.
From: vanekl
Subject: Re: A few more Package/Symbol Questions
Date:
Message-ID: <fu0sqn$oio$1@aioe.org>
dstein64 wrote:
snip
> Lastly, how can symbols, or the elements they refer to, be deleted?
> Basically, if I used (defvar x 10), is there any way to basically undo
> this, as if it had never been set?
>
> Thanks.
makunbound?
vanekl wrote:
> dstein64 wrote:
> snip
>> Lastly, how can symbols, or the elements they refer to, be deleted?
>> Basically, if I used (defvar x 10), is there any way to basically undo
>> this, as if it had never been set?
>>
>> Thanks.
>
> makunbound?
and fmakunbound for defun/defmacro.
Use UNINTERN to clear the name from the symbol table.
To delete a referenced element, (setf x nil).
- Daniel
"dstein64" <········@gmail.com> wrote in message
·········································@f36g2000hsa.googlegroups.com...
>I just have a few more questions on the subject.
>
> First, suppose I have a structure such that:
> (defstruct car color year)
> And I create some car structures. Is there any way to have a newly
> created symbol refer to one of these attributes of a specific car? For
> example, suppose I want to redefine the color of a car. I would use
> (setf (car-color somecar) blue).
> Is there any way to have a symbol refer to (car-color somecar), so
> that I can use (setf *somesymbol* blue), and it would update the value
> of the car.
A symbol macro will do this.
CL-USER 10 > (defstruct car color year)
CAR
CL-USER 11 > (setf somecar (make-car))
#S(CAR :COLOR NIL :YEAR NIL)
CL-USER 12 > (define-symbol-macro *somesymbol* (car-color somecar))
*SOMESYMBOL*
CL-USER 13 > (setf *somesymbol* 'blue)
BLUE
CL-USER 14 > somecar
#S(CAR :COLOR BLUE :YEAR NIL)
> Is there any simplified way to export all the structure functions from
> a package. When I use (defstruct car color year), it comes with a
> whole bunch of functions for easily modifying the structure. Is there
> any way that all these can be easily exported from the package that
> the structure was defined in?
(export
(loop for s being the symbols in 'your-package
when (search "car" (symbol-name s) :test #'equalp)
collect s))
> Lastly, how can symbols, or the elements they refer to, be deleted?
> Basically, if I used (defvar x 10), is there any way to basically undo
> this, as if it had never been set?
CL-USER 39 > (defvar x 10)
X
CL-USER 40 > x
10
CL-USER 41 >
(progn (makunbound 'x) (unintern 'x))
T
CL-USER 42 > x
Error: The variable X is unbound.
1 (continue) Try evaluating X again.
Carl Taylor
In article <···················@bgtnsc05-news.ops.worldnet.att.net>,
"Carl Taylor" <··········@att.net> wrote:
> > Lastly, how can symbols, or the elements they refer to, be deleted?
> > Basically, if I used (defvar x 10), is there any way to basically undo
> > this, as if it had never been set?
>
> CL-USER 39 > (defvar x 10)
> X
>
> CL-USER 40 > x
> 10
>
> CL-USER 41 >
> (progn (makunbound 'x) (unintern 'x))
> T
>
> CL-USER 42 > x
>
> Error: The variable X is unbound.
> 1 (continue) Try evaluating X again.
It is important to understand that uninterning a symbol is not the same
as deleting it:
? (defvar x 10)
X
? (setf y 'x)
X
? (symbol-value 'y)
X
? (symbol-value y)
10
? (unintern 'x)
T
? y
#:X
? (symbol-value y)
10
?
If you don't understand this, read:
http://www.flownet.com/ron/packages.pdf
rg