gensym question

Discussion of Common Lisp
Post Reply
abc
Posts: 4
Joined: Fri Sep 18, 2009 7:09 am

gensym question

Post by abc » Fri Sep 18, 2009 7:19 am

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.

skypher
Posts: 34
Joined: Thu Jul 03, 2008 6:12 am

Re: gensym question

Post by skypher » Sun Sep 20, 2009 1:20 am

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.

findinglisp
Posts: 447
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX
Contact:

Re: gensym question

Post by findinglisp » Sun Sep 20, 2009 1:26 am

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/

abc
Posts: 4
Joined: Fri Sep 18, 2009 7:09 am

Re: gensym question

Post by abc » Sun Sep 20, 2009 7:47 am

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

Post Reply