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

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

Re: Replacement for Common Lisp's GENSYM in Emacs Lisp


From: Stefan Monnier
Subject: Re: Replacement for Common Lisp's GENSYM in Emacs Lisp
Date: Tue, 04 May 2010 15:41:47 -0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.93 (gnu/linux)

> This is actually a non-trivial problem.  Best approach would probably be
> to do something similar to how they generate universally unique IDs.

The best approach often is to not use "unique symbols" at all.  A common
technique is to rely on lexical scoping to precisely control which
variables are visible to each part of a macro's argument during its
evaluation.  E.g.:

  (defmacro dotimes (xl e)
    (let ((x (car xl))
          (l (cadr xl)))
      `(let ((body (lambda (,x) ,e))
             (list ,l))
         (while (consp list)
           (funcall body (car list))
           (setq list (cdr list))))))

Notice how the expressions `l' and `e' are evaluated in a context where
neither of `body', nor `list' exist.  No need for gensym.

Of course Elisp's lack of good support for lexical scoping makes such
a technique impractical.  But `make-symbol' can be used instead of
`gensym' and works very well for that purpose.


        Stefan


reply via email to

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