From: �x���u
Subject: about (defun (setf primo) (val lst)..)
Date: 
Message-ID: <MPG.ff1bf29cc37d4c1989683@news.ntu.edu.tw>
in below source code

(defun (setf primo) (val lst)
     (setf (car lst) val))

run
(let((x(list'a 'b 'c)))
     (setf (primo x) 480) 
     x)
(480 B C)

so if we write
(defun (setf pp) (p1 p2)
   (format t "~%p1: ~A~%p2: ~A" p1 p2))

(setf (pp 3) 4)
p1: 4
p2: 3

for what goals does we should to use this odd form ?
or does it has specail advantages ?
can someone else describe it or just write a source code to specify its 
usage.

thanks.
Dick Guan

From: David Bakhash
Subject: Re: about (defun (setf primo) (val lst)..)
Date: 
Message-ID: <cxjyauwrpim.fsf@hawk.bu.edu>
········@csie.ntu.edu.tw (�x���u) writes:

> in below source code
> 
> <snip of the ``defun (setf ...)'' stuff
> 
> for what goals does we should to use this odd form ?
> or does it has specail advantages ?
> can someone else describe it or just write a source code to specify its 
> usage.

hey,

You're confused about something which is very confusing to begin
with.  I'll try to help clear this up for you as best I can.

Originally, when a programmer made a new data structure, with new
accessor functions, they wanted their own setf methods too.  They used 
to use `defsetf', and I am sure some still do.  But the more recent
way, and better way in my opinion is to use defun and setf.  Let me
give you an example.

Say we have a new data structure which stores 3 things (kinda like a
cons, but 3 instead of two).  We can do something like this:

(defun cons3 (a b c)
  (cons a (cons b c)))

So far so good.  Now, let's we have 3 accessor functions: uno, dos,
and tres (i.e. like `first', `second', and `third').  Then you might
define these accessor functions like this:

(defun uno (c3)
  (car c3))

(defun dos (c3)
  (car (cdr c3)))

(defun tres (c3)
  (cdr (cdr c3)))


So say now I make an instance of one of these guys:

(setq m (cons3 'I 'LOVE 'LISP))

I can easily get the second ("dos") value of m:

(dos m)

==> LOVE

But what if we want to set the value of the second `dos' part of m?
We'd like to do this:

(setf (dos m) 'ADORE)

so that if we now did (dos m) we'd get ADORE.  So this is how you do
it with defun and setf:

(defun (setf dos) (newval c3)
  (setf (cadr c3) newval))

now, if we do this:

(setf (dos m) 'ADORE)

and evaluate m, then we should get:

m

==> (I ADORE . LISP)

Okay?  With setf methods, you never have to remember the names of both 
the accessor and the mutator functions.  the accessor function name
will suffice.

take care,
dave
From: Barry Margolin
Subject: Re: about (defun (setf primo) (val lst)..)
Date: 
Message-ID: <vgZh1.50$ir6.1016682@cam-news-reader1.bbnplanet.com>
In article <···············@hawk.bu.edu>, David Bakhash  <·····@bu.edu> wrote:
>Originally, when a programmer made a new data structure, with new
>accessor functions, they wanted their own setf methods too.  They used 
>to use `defsetf', and I am sure some still do.  But the more recent
>way, and better way in my opinion is to use defun and setf.  Let me
>give you an example.

The main reason why the (SETF <name>) function name was invented was for
CLOS, as it allows you to use it as the name of a generic function, and
define CLOS methods for it.  Once that was in place, we decided to
generalize it, so they work in all places where you can use symbols as
function names, e.g. DEFUN, FDEFINITION, FUNCTION, etc.

The nice thing about this is that SETF functions are first-class objects,
unlike the macro-like SETF expanders.  You can write:

(funcall (if (eq *foo-or-bar* 'foo)
             #'(setf foo)
             #'(setf bar))
         x y)

On the other hand, they're more limited in what they can do; you can't use
a SETF function to do what (setf (ldb <bytespace> <variable>) <value>)
does, because it has to expand into something that modifies the value of
<variable>, and functions receive their arguments already evaluated.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
From: David Bakhash
Subject: Re: about (defun (setf primo) (val lst)..)
Date: 
Message-ID: <cxjwwafa06d.fsf@hawk.bu.edu>
Barry Margolin <······@bbnplanet.com> writes:

> On the other hand, they're more limited in what they can do; you can't use
> a SETF function to do what (setf (ldb <bytespace> <variable>) <value>)
> does, because it has to expand into something that modifies the value of
> <variable>, and functions receive their arguments already evaluated.

yeah.  So in this case, there's still defsetf, right?

god.  macros are a pain.  no doubt about it.

dave