This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

gensym question

4 posts · 1393 views

Hi all,

I'm learning Lisp by reading Practical Common Lisp. I have a question regarding gensym.

The book presents on chapter 9 the macro with-gensyms:
(defmacro with-gensyms ((&rest names) &body body)
  `(let ,(mapcar (lambda (x) `(,x (gensym))) names) ,@body))
It's fine. I understand how it works. I would like to know if there's any drawback in reimplementing it like this:
(defmacro with-gensyms ((&rest names) &body body)
  `(let ,(mapcar (lambda (x) `(,x ',(gensym))) names) ,@body))
I think it's a little more efficient as gensym only gets called during the macroexpansion, not at runtime. However, being at a very early stage of learning Lisp (10 days), I'm not sure if it is dangerous to call gensym like this for some reason.

Thanks in advance.

Re: gensym question

This won't work. Remember the point of GENSYM is to give you a fresh and unique interned symbol; if you're calling it at mexp time you will get the same symbol over and over.

Re: gensym question

abc wrote:I think it's a little more efficient as gensym only gets called during the macroexpansion, not at runtime. However, being at a very early stage of learning Lisp (10 days), I'm not sure if it is dangerous to call gensym like this for some reason.
As skypher said, this won't work. Remember that with-gensyms is a macro for writing other macros. Thus, the "runtime" of with-gensyms is during the macro-expansion time of another macro, not during that other macro's runtime. Ignoring the fact that it won't work, as skypher said, it's typically not a big deal to shave off a few milliseconds of processing during macro-expansion time, since that's often done only once per macro form and often as part of compile time.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

Re: gensym question

Cool, thanks for your clear replies. Now I understand the issue correctly.