help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: lisp style question


From: RG
Subject: Re: lisp style question
Date: Sun, 05 Dec 2010 21:45:59 -0800
User-agent: MT-NewsWatcher/3.5.1 (Intel Mac OS X)

In article <87aakjkg2i.fsf@kuiper.lan.informatimago.com>,
 "Pascal J. Bourguignon" <pjb@informatimago.com> wrote:

> RG <rNOSPAMon@flownet.com> writes:
> 
> > In article 
> > <6e2fe51c-ff4c-429f-b221-a3cbe23e958f@n2g2000pre.googlegroups.com>,
> >  Katalin Sinkov <lispstylist@gmail.com> wrote:
> >
> >> how to conveniently costruct the list that goes with getf ?
> >
> > You can't construct anything with GETF.  GETF is an accessor.
> 
> This is not correct.  GETF is special:
> 
> CL-USER> (let ((plist '()))
>            (setf (getf plist :k1) 1
>                  (getf plist :k2) 2)
>            plist)
> (:K2 2 :K1 1)
> 
> Setfers are able to do such things.

GETF is not unique in this regard.  GETHASH works the same way.  But 
relying on GETF's ability to allocate storage for new keys is fraught 
with peril, e.g.:

(defun make-new-plist () (list :key1 :value1))

(defun set-key (plist key value)
  (setf (getf plist key) value))

(defun foo ()
  (let* ((plist (make-new-plist)))
    (set-key plist :key1 :new-value-1)
    (set-key plist :key2 :value2)
    plist))

(foo) ==> (:KEY1 :NEW-VALUE-1)  ; What happened to key2?

I think it's better to advise beginners to avoid such things rather than 
try to explain the sometimes subtle distinction between places and 
first-class mutable data objects.

rg


reply via email to

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