emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Contributing (setf (assoc ...))


From: David Kastrup
Subject: Re: Contributing (setf (assoc ...))
Date: Wed, 18 Nov 2009 16:00:35 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

Stefan Monnier <address@hidden> writes:

>> (defsetf assoc (key place) (value)
>>   (let ((s1 (gensym)))
>>     `(let ((,s1 (assoc ,key ,place)))
>>        (if ,s1 (setf (second ,s1) ,value)
>>          (push (list ,key ,value) ,place)))))
>
> This code has 2 problems:
> 1- association lists have elements of the form (KEY . VAL) rather than
>    (KEY VAL), so rather than (list ,key ,value) it should use
>    (cons ,key ,value) and rather than (second ,s1) it should use
>    (cdr ,s1).
> 2- it's unsatisfactory iin that it's asymmetric w.r.t assoc; because
>    assoc returns not just the VAL associated to a KEY but the whole
>    (KEY . VAL).
>    Usually (setf <foo> <bar>) should imply that a subsequent evaluation
>    of <foo> should return the value of <bar>, but here this can't be the
>    case.  IOW `assoc' is inherently incompatible with setf.
>
> Problem 1 is trivial, obviously.  Problem 2 is more philosophical than
> anything, but it makes the macro unsatifactory.
>
> WDPT?

The above does not have the right setf semantics.  One would need to
write

(setf (cdr (assoc key ...)) value)

in order to set just the value of a key value pair.  If one does, it is
perfectly fine that the return value is just value.

Or one would have to use something like

(setf (assoc-default key ...) value)

-- 
David Kastrup





reply via email to

[Prev in Thread] Current Thread [Next in Thread]