From: Jimka
Subject: using setf with assoc
Date: 
Message-ID: <1135857852.899275.295770@g14g2000cwa.googlegroups.com>
I noticed that (setf (assoc key alist) newvalue) fails,
and in sbcl I get the following warning:
;   The function (SETF ASSOC) is undefined, and its name is reserved by
ANSI CL so
;   that even if it were defined later, the code doing so would not be
portable.

Does anyone know why this is not implemented?  Is it because noone
could
agree about what it would do?  Or does it really not make sense
in the first place?

From: Luís Oliveira
Subject: Re: using setf with assoc
Date: 
Message-ID: <m2irt8rshm.fsf@deadspam.com>
"Jimka" <·····@rdrop.com> writes:
> I noticed that (setf (assoc key alist) newvalue) fails,

CL-USER> (defvar *alist* (list (cons 'key1 'value1) (cons 'key2 'value2)))
*ALIST*
CL-USER> *alist*
((KEY1 . VALUE1) (KEY2 . VALUE2))
CL-USER> (assoc 'key2 *alist*)
(KEY2 . VALUE2)
CL-USER> (setf (cdr (assoc 'key2 *alist*)) 'another-value)
ANOTHER-VALUE
CL-USER> (assoc 'key2 *alist*)
(KEY2 . ANOTHER-VALUE)


> Does anyone know why this is not implemented?  Is it because noone
> could agree about what it would do?  Or does it really not make sense
> in the first place?

I'm not sure what (setf (assoc <key> <alist>) <value>) would mean.
Something like this?

  (setf <alist> (delete <key> <alist> :key #'car))
  (push <alist> <value>) ; so value should be an assoc pair

-- 
Luís Oliveira
luismbo (@) gmail (.) com
Equipa Portuguesa do Translation Project
http://www.iro.umontreal.ca/translation/registry.cgi?team=pt
From: Steven M. Haflich
Subject: Re: using setf with assoc
Date: 
Message-ID: <073tf.40361$BZ5.30174@newssvr13.news.prodigy.com>
Jimka wrote:
> I noticed that (setf (assoc key alist) newvalue) fails,
> and in sbcl I get the following warning:
> ;   The function (SETF ASSOC) is undefined, and its name is reserved by
> ANSI CL so
> ;   that even if it were defined later, the code doing so would not be
> portable.
> 
> Does anyone know why this is not implemented?  Is it because noone
> could
> agree about what it would do?  Or does it really not make sense
> in the first place?

It does not make sense:

setf of assoc is not specified in the ANS because there is no way to
specify it that would be consistent with the language conventions.

  - It is required that a setf return the new value stored into the
    <place>.
  - assoc returns a cons of a key and value.  A cons is not reasonably
    defined as a place.  You could, I suppose, have the setf method
    accept a fresh cons, but other code could have previously retrieved
    and cached that cons from the alist, so providing a new cons would
    violate the venerable semantics of alists.  Once a key is consed
    onto an alist, that cons object remains unless explicitly removed
    from the alist.

If you don't understand this you're not quite ready to write setf
methods.