From: Greg Menke
Subject: macrolet
Date: 
Message-ID: <m37kbyyesm.fsf@europa.pienet>
Hi,

I've been doing some work with macrolet and I was wondering why CL
doesn't specifically allow the use of local variables and/or function
bindings within the body similarly to how it does with flet.  The
hyperspec says the result of such usage is undefined, so I was also
wondering if any implementations went to the trouble of making it
work.

Thanks,

Greg Menke

From: Barry Margolin
Subject: Re: macrolet
Date: 
Message-ID: <vy94a.6$qh3.392@paloalto-snr1.gtei.net>
In article <··············@europa.pienet>,
Greg Menke  <··········@toadmail.com> wrote:
>I've been doing some work with macrolet and I was wondering why CL
>doesn't specifically allow the use of local variables and/or function
>bindings within the body similarly to how it does with flet.  The
>hyperspec says the result of such usage is undefined, so I was also
>wondering if any implementations went to the trouble of making it
>work.

Because macros are normally expanded at compile time, but local variables
don't get bound until run time.

-- 
Barry Margolin, ··············@level3.com
Genuity Managed Services, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Nils Goesche
Subject: Re: macrolet
Date: 
Message-ID: <lyu1f2hhxt.fsf@cartan.de>
Greg Menke <··········@toadmail.com> writes:

> I've been doing some work with macrolet and I was wondering why CL
> doesn't specifically allow the use of local variables and/or
> function bindings within the body similarly to how it does with
> flet.  The hyperspec says the result of such usage is undefined, so
> I was also wondering if any implementations went to the trouble of
> making it work.

That's because the macros are expanded at compile time.  Local
variables outside the MACROLET form don't have values until run-time.
That doesn't mean you can't use local variables altogether, though,
you only have to be aware which exist at run time and which at compile
time.  This is legal, for instance:

(defun foo (x y)
  (let ((ret 0))
    (macrolet ((bar (a b)
		 (let ((z `(* 3 ,a)))
		   `(setq ret (+ ret ,z ,b)))))
      (bar x y)
      (bar y x))
    ret))

It is essentially the same as

(defun foo (x y)
  (let ((ret 0))
    (setq ret (+ ret (* 3 x) y))
    (setq ret (+ ret (* 3 y) x))
    ret))

The variables A, B and Z are bound at compile time, X, Y and RET at
run time, when you call FOO.

That's how I interpret it, anyway.

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: Greg Menke
Subject: Re: macrolet
Date: 
Message-ID: <m3y94ewx5n.fsf@europa.pienet>
Nils Goesche <······@cartan.de> writes:

> Greg Menke <··········@toadmail.com> writes:
> 
> > I've been doing some work with macrolet and I was wondering why CL
> > doesn't specifically allow the use of local variables and/or
> > function bindings within the body similarly to how it does with
> > flet.  The hyperspec says the result of such usage is undefined, so
> > I was also wondering if any implementations went to the trouble of
> > making it work.
> 
> That's because the macros are expanded at compile time.  Local
> variables outside the MACROLET form don't have values until run-time.
> That doesn't mean you can't use local variables altogether, though,
> you only have to be aware which exist at run time and which at compile
> time.  This is legal, for instance:
> 
> (defun foo (x y)
>   (let ((ret 0))
>     (macrolet ((bar (a b)
> 		 (let ((z `(* 3 ,a)))
> 		   `(setq ret (+ ret ,z ,b)))))
>       (bar x y)
>       (bar y x))
>     ret))
> 
> It is essentially the same as
> 
> (defun foo (x y)
>   (let ((ret 0))
>     (setq ret (+ ret (* 3 x) y))
>     (setq ret (+ ret (* 3 y) x))
>     ret))
> 
> The variables A, B and Z are bound at compile time, X, Y and RET at
> run time, when you call FOO.
> 
> That's how I interpret it, anyway.

OK, that helps a lot.  Once again the subtleties of the dual nature of
macros tripped me up.  I was trying to control the macro expansion at
runtime.

Thanks,

Gregm