Page 1 of 1

gensym question

Posted: Fri Sep 18, 2009 7:19 am
by abc
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:

Code: Select all

(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:

Code: Select all

(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

Posted: Sun Sep 20, 2009 1:20 am
by skypher
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

Posted: Sun Sep 20, 2009 1:26 am
by findinglisp
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.

Re: gensym question

Posted: Sun Sep 20, 2009 7:47 am
by abc
Cool, thanks for your clear replies. Now I understand the issue correctly.