gensym question

Discussion of Common Lisp

gensym question

Postby 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.
abc
 
Posts: 4
Joined: Fri Sep 18, 2009 7:09 am

Re: gensym question

Postby 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.
skypher
 
Posts: 34
Joined: Thu Jul 03, 2008 6:12 am

Re: gensym question

Postby 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/
findinglisp
 
Posts: 440
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX

Re: gensym question

Postby abc » Sun Sep 20, 2009 7:47 am

Cool, thanks for your clear replies. Now I understand the issue correctly.
abc
 
Posts: 4
Joined: Fri Sep 18, 2009 7:09 am


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 6 guests

cron