[Top][All Lists]

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

Re: Basic questions about elisp

From: Pascal J. Bourguignon
Subject: Re: Basic questions about elisp
Date: Thu, 05 Nov 2009 15:58:42 +0100
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

Francis Moreau <address@hidden> writes:
> I did the following macro to do that, although I'm not sure it's the
> good thing to do:
>   (defmacro x-nconc (l e)
>     `(if (null ,l) (setq ,l ,e) (nconc ,l ,e)))

It's ok, but it works only on variables.  If you want your macro to
work on places too, you could use setf instead of setq (actually, you
could always use setf instead of setq):

(require 'cl)

(defmacro* nconcf (place expression &environment environment)
  (destructuring-bind (vars vals store-vars writer-form reader-form)
       (get-setf-method place environment)
    (when (rest store-vars) (error "Cannot expand %S" place))
    `(let ,(mapcar* (function list) vars vals)
       (let ((,(first store-vars) ,reader-form))
         (setq ,(first store-vars) (nconc ,(first store-vars) ,expression))

(let ((a (make-vector 3 '()))
      (i 0))
 (nconcf (aref a (incf i)) '(x))
 (nconcf (aref a (decf i)) '(y))
 (nconcf (aref a (incf i)) '(x))
--> [(y) (x x) nil]

__Pascal Bourguignon__

reply via email to

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