From: Marcel van gerven
Subject: setf problem
Date: 
Message-ID: <bmm77e$cbj$1@wnnews.sci.kun.nl>
Yet another question from a newbie CL programmer..

Suppose I define:

>> (setf z '(a b c))
>> (defun nl (i lis) (nth i lis))

then

>> (setf (nth 1 z) 'd)

will give 

z = (a d c)

but 

>> (setf (nl 1 z) 'e)

will give 'nl' is not fbound

what is happening here????

thx,
Marcel van Gerven 

From: Edi Weitz
Subject: Re: setf problem
Date: 
Message-ID: <87y8vlp9l6.fsf@bird.agharta.de>
On Thu, 16 Oct 2003 15:44:10 +0200, Marcel van gerven <········@cs.kun.nl> wrote:

> Yet another question from a newbie CL programmer..
> 
> Suppose I define:
> 
> >> (setf z '(a b c))
> >> (defun nl (i lis) (nth i lis))
> 
> then
> 
> >> (setf (nth 1 z) 'd)
> 
> will give 
> 
> z = (a d c)
> 
> but 
> 
> >> (setf (nl 1 z) 'e)
> 
> will give 'nl' is not fbound
> 
> what is happening here????

You have to define this function if you want to have it:

  * (defun nl (i list) (nth i list))

  NL
  * (defun (setf nl) (new-value i list) (setf (nth i list) new-value))

  (SETF NL)
  * (let ((z '(a b c)))
      (print z)
      (setf (nth 1 z) 'd)
      (print z)
      (setf (nl 1 z) 'e)
      (print z)
      (values))

  (A B C)
  (A D C)
  (A E C)

You'll also want to look at DEFSETF and DEFINE-SETF-EXPANDER:

  <http://www.lispworks.com/reference/HyperSpec/Body/m_defset.htm>
  <http://www.lispworks.com/reference/HyperSpec/Body/m_defi_3.htm>

Edi.
From: Wolfhard Buß
Subject: Re: setf problem
Date: 
Message-ID: <m3n0c1gh5c.fsf@buss-14250.user.cis.dfn.de>
* Edi Weitz writes:

>   * (defun nl (i list) (nth i list))
> 
>   NL
>   * (defun (setf nl) (new-value i list) (setf (nth i list) new-value))
> 
>   (SETF NL)
>   * (let ((z '(a b c)))
>       (print z)
>       (setf (nth 1 z) 'd)
>       (print z)
>       (setf (nl 1 z) 'e)
>       (print z)
>       (values))
> 
>   (A B C)
>   (A D C)
>   (A E C)

Hamburg incarnation of undefined consequences?


-- 
"Hurry if you still want to see something. Everything is vanishing."
                                       --  Paul C�zanne (1839-1906)
From: Edi Weitz
Subject: Re: setf problem
Date: 
Message-ID: <87k775nhtq.fsf@bird.agharta.de>
On 16 Oct 2003 20:40:47 +0200, ·····@gmx.net (Wolfhard Bu�) wrote:

> * Edi Weitz writes:
> 
> >   * (let ((z '(a b c)))
> >       (print z)
> >       (setf (nth 1 z) 'd)
> 
> Hamburg incarnation of undefined consequences?

Blush.

"It was just an example," he was heard babbling before he vanished...
From: Kaz Kylheku
Subject: Re: setf problem
Date: 
Message-ID: <cf333042.0310161554.6ea1825e@posting.google.com>
Marcel van gerven <········@cs.kun.nl> wrote in message news:<············@wnnews.sci.kun.nl>...
> Yet another question from a newbie CL programmer..
> 
> Suppose I define:
> 
> >> (setf z '(a b c))
> >> (defun nl (i lis) (nth i lis))
> 
> then
> 
> >> (setf (nth 1 z) 'd)

... is undefined behavior! The z variable is bound to a literal list
object derived from the program's source code by the QUOTE operator!

> but 
> 
> >> (setf (nl 1 z) 'e)
> 
> will give 'nl' is not fbound
>
> what is happening here????

(nl 1 z) is just a function call, not an assignable place understood
by SETF. You can turn function calls into assignable places. The
simplest way is to provide a complementary pair of functions: one
which reads, and one which stores. The store function has the same
arguments as the reader, plus one more argument which specifies the
new value to be stored.

Then you write a magic incantation which teaches the SETF macro to use
these two functions to create a virtual storage place.

  ;;; the functions:

  (defun get-something (arg1 arg2 arg3 ...) ...)

  (defun set-something (arg1 arg2 arg3 ... new-value) ...)

  ;;; the magic incantation so you can write expressions
  ;;; like (setf (get-something arg1 arg2 arg3 ...) new-value):

  (defsetf get-something set-something)

Note that there are more more powerful ways to customize SETF in
addition to DEFSETF; but DEFSETF is very simple to use, designed
specifically for the case when you just want to turn a read accessor
function into an assignable place.
From: Stavros Macrakis
Subject: Re: setf problem
Date: 
Message-ID: <iaKjb.14379$fv4.8769@nwrdny02.gnilink.net>
"Marcel van gerven" <········@cs.kun.nl> wrote in message
·················@wnnews.sci.kun.nl that (setf (nth 1 z) 'd) works as
expected, but (defun nl (i lis) (nth i lis)) followed by (setf (nl 1 z) 'e)
does not, and asks "what is happening here????"

Your example would work if Lisp supported some sort of first-class L-value,
which it doesn't.  First-class L-values are, anyway, problematic at best.
For example, what do you expect the following code to do:

    (defun blah () some-global-variable)
    (setf (blah) 23) ; would modify some-global-variable  (NOT normal
semantics)

Do you really want the setf to change the value of some-global-variable?  If
not, are you willing to explicitly convert L-values to R-values all over the
place, e.g.:

    (defun blah () (pure-value some-global-variable)
    (setf (blah) 23)  ; would cause dynamic error

Some languages in the past have supported very general L-values (though not
fully first-class).  If I'm not mistaken, Algol 60 allowed a function call
to

    f(a)

to modify a (call by value) and Algol 68 allowed you to say things like

   (if test then a else b) := newval

But Lisp does not.  Yes, I do know you could say (f 'a) and (set (if test 'a
'b) newval), but that is not the same, since you are using a different
assignment operator (set rather than setq) and are explicitly marking 'a and
'b as quoted, so you can't use the result of that if where the *value* of a
or b is wanted.

For more on this subject, do some Google searches on lvalue, thunk,
call-by-name, etc.

As for what setf actually does, it is a *macro* and thus a *static*
mechanism.  When you write (setf (somefunc args) newval), it is really just
syntactic sugar for writing (somefunc-setter args newval), and you must
write somefunc-setter explicitly.  Actually a bit more complicated, but that
is basically it.

             -s
From: Sebastian Stern
Subject: Re: setf problem
Date: 
Message-ID: <ad7d32de.0310170558.1285f91b@posting.google.com>
Marcel van gerven <········@cs.kun.nl> wrote in message news:<············@wnnews.sci.kun.nl>...
> Yet another question from a newbie CL programmer..
> (...)

See also the thread http://groups.google.nl/groups?hl=nl&lr=&ie=UTF-8&oe=UTF-8&selm=8ljmq0%248t2%241%40nnrp1.deja.com

Sebastian Stern