From: T. V. Raman
Subject: pointer variables in common lisp:
Date: 
Message-ID: <1992Aug3.144211.14384@cs.cornell.edu>
 I would like to implement a set of functions that will provide the
following functionality:

(setf x 1)

(setf y x)

(setf x 2)

should result in changing the value of y to 2. 

Of course I do not want to change the behaviour of setf itself to
this, since it is in general not what I want all the time. 

How would I implement something like this cleanly? 

The only thing I can think of is to create a structure for every
variable that I need to do this to and then exploit the fact that setf
does not copy structures.

Is there a cleaner solution?

Is the trick of using structures likely to be bullet proof, and
further is it good coding style?

Thanks,

--Raman
-- 
   T. V. Raman <·····@cs.cornell.edu>Tel: (607)255-7421 R 272-2435
		       Office: 5162 Upson Hall,
Department of Computer Science, Cornell University Ithaca NY 14853-6201
		Res: 226 Bryant Avenue Ithaca NY 14850

From: Gregor Kiczales
Subject: Re: pointer variables in common lisp:
Date: 
Message-ID: <GREGOR.92Aug3124959@tracer-bullet.parc.xerox.com>
If it matters to you that the syntax look like they are just variable
references, you may want to consider using symbol-macrolet, which was
created just for this sort of thing.

Gregor
From: Thomas A. Russ
Subject: Re: pointer variables in common lisp:
Date: 
Message-ID: <22109@venera.isi.edu>
In article ... ·····@cs.cornell.edu (T. V. Raman) writes:


  >  I would like to implement a set of functions that will provide the
  > following functionality:
  > 
  > (setf x 1)
  > 
  > (setf y x)
  > 
  > (setf x 2)
  > 
  > should result in changing the value of y to 2. 
  > 

First of all, all lisp variables are already pointer variables.  That
is why lisp behaves the way it does.  All you do with SETF is to
change where the lisp variable is pointing.  If you want to have
another level of indirection, you will need to explicitly handle the
dereferencing yourself.

One solution:

(setf x 1)

(setf y 'x)   ; Note the QUOTE!
              ;  You want y to point at X, not the value of X.

(symbol-value y) ; ==> 1

(setf x 2)

(symbol-value y) ; ==> 2
y		 ; ==> X


To get the dereferenced value of what y is pointing at you will need
to call the function SYMBOL-VALUE on the value of y.
--

Thomas A. Russ                                             ···@isi.edu    
USC/ISI, 4676 Admiralty Way, Marina del Rey, CA 90292      (310) 822-1511
From: Barry Margolin
Subject: Re: pointer variables in common lisp:
Date: 
Message-ID: <15jum4INNcs5@early-bird.think.com>
In article <·····················@cs.cornell.edu> ·····@cs.cornell.edu (T. V. Raman) writes:
>(setf x 1)
>
>(setf y x)
>
>(setf x 2)
>
>should result in changing the value of y to 2. 

>The only thing I can think of is to create a structure for every
>variable that I need to do this to and then exploit the fact that setf
>does not copy structures.

Of course, that changes how you do the assignment significantly.  It would
become something like

(setf x (make-container :contents 1))

(setf y x)

(setf (container-contents x) 2)

Now the values of both (container-contents x) and (container-contents y)
will be 2.  In other words, you have to distinguish assignment of the
reference and assignment of the contents.  This is like pointer variables
in many other languages.  If you'd like to emulate something like C++'s
reference variables, though (which is what your original example looks
like) then this won't do it (Symbolics Lisp Machines have hardware that
supports "forwarding pointers" that can be used to set up such links, but
that's certainly not very clean or portable).

>Is the trick of using structures likely to be bullet proof, and
>further is it good coding style?

It's definitely bullet proof.  I think that it's reasonable style, since
the required syntax makes the linking quite explicit.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar
From: Clinton Hyde
Subject: Re: pointer variables in common lisp:
Date: 
Message-ID: <CHYDE.92Aug3141205@pecos.ads.com>
this is of course, imho, but I would recommend NOT using SETF with
simple variables. it's generally OK as far as I know, but I'd say it's
better to use SETQ on simple variables. SETF is appropriate for more
complex references, anytime when the first arg is NOT a simple
variable. 

I doubt you could even manage to modify SETF if you wanted. on a
LispMachine (a REAL lisp computer), what you want to do is actually
fairly easy.

for your circumstances, a defstruct is probably the best truly
portable approach.

 -- clint
--

Clint Hyde		"Give me a LispM or give me death!" -- anonymous

Advanced Decision Systems	Internet:  ·····@chesapeake.ads.com
2111 Wilson Blvd #800
Arlington, VA 22201		(703) 875-0327