From: Guybrush Threepwood
Subject: gensym in macro definition
Date: 
Message-ID: <pan.2006.08.31.04.18.53.777482@hotmail.com>
I'm making exercises on macros and this is one I found in the archives of
the group.

;;; Define a macro n-of that takes a number n and an expression, and
;;; returns a list of n successive values returned by the expression

(defmacro n-of (n expression)
  (let ((i (gensym)))
    `(loop for ,i from 1 to ,n
           collect ,expression)))

I wonder if the gensym is really necessary. According to the rules for
detecting variable capture in Graham's On Lisp I think it is. Am I right?

(If anyone can state some fun macro exercises, please do so.)

-- 
"Don't worry about people stealing your ideas. If your ideas are any
good, you'll have to ram them down people's throats."
	-- Howard Aiken

From: Kaz Kylheku
Subject: Re: gensym in macro definition
Date: 
Message-ID: <1157000224.042080.80270@h48g2000cwc.googlegroups.com>
Guybrush Threepwood wrote:
> (defmacro n-of (n expression)
>   (let ((i (gensym)))
>     `(loop for ,i from 1 to ,n
>            collect ,expression)))
>
> I wonder if the gensym is really necessary.

Yes it is, since the loop variable is in the scope of the expression.
If you just call it X, and the expression contains a reference to X,
that reference will go to that X instead of the one the programmer
wants to reach:

 (defmacro n-of (n expression)
    `(loop for x from 1 to ,n
           collect ,expression))

  (let ((x 42))
    (n-of 10 x))

  -> (1 2 3 4 5 6 7 8 9 10)  ; surprise!!!

Have you considered actually trying it both ways? One way to
demonstrate that the gensym is needed is not to put one in, and then
show a miscalculation like the above.
From: Stephen Compall
Subject: Re: gensym in macro definition
Date: 
Message-ID: <VYtJg.775651$tQ4.507146@fe01.news.easynews.com>
Guybrush Threepwood wrote:
> (defmacro n-of (n expression)
>   (let ((i (gensym)))
>     `(loop for ,i from 1 to ,n
>            collect ,expression)))
> 
> I wonder if the gensym is really necessary. According to the rules for
> detecting variable capture in Graham's On Lisp I think it is. Am I right?

It is necessary for the code you have output.  For extra fun find the
loop-clause that will let LOOP do this without an explicit variable.

-- 
Stephen Compall
http://scompall.nocandysw.com/blog
From: Guybrush Threepwood
Subject: Re: gensym in macro definition
Date: 
Message-ID: <pan.2006.08.31.05.30.52.607209@hotmail.com>
On Thu, 31 Aug 2006 04:55:17 +0000, Stephen Compall wrote:

> Guybrush Threepwood wrote:
>> (defmacro n-of (n expression)
>>   (let ((i (gensym)))
>>     `(loop for ,i from 1 to ,n
>>            collect ,expression)))
>> 
>> I wonder if the gensym is really necessary. 
> 
> It is necessary for the code you have output.  For extra fun find the
> loop-clause that will let LOOP do this without an explicit variable.

(defmacro n-of2 (n expression)
  `(loop repeat ,n
         collect ,expression))

-- 
"Don't worry about people stealing your ideas. If your ideas are any
good, you'll have to ram them down people's throats."
	-- Howard Aiken
From: Larry Clapp
Subject: Re: gensym in macro definition
Date: 
Message-ID: <slrnefejkv.ofg.larry@theclapp.ddts.net>
On 2006-08-31, Guybrush Threepwood <·······@hotmail.com> wrote:
> (If anyone can state some fun macro exercises, please do so.)

http://www.paulgraham.com/onlisp.html

HHOS.  :)

-- L