From: Ulrich Hobelmann
Subject: Recursion in Macros
Date: 
Message-ID: <41r3cgF1g2eiuU1@individual.net>
Hi, can Lisp macros call themselves recursively?

I tried something like `(foo ,(blarb ...)) inside a blarb macro, and get 
some kind of error (telling me that blarb isn't a function).

I rewrote the macro to simply call a recursive function that does the 
macro-expansion, and it works, but it doesn't seem too elegant...

-- 
the bottom line is that a JavaSchool that won't teach C and won't teach
Scheme is not really teaching computer science, either.  --  Joel Spolsky

From: Barry Margolin
Subject: Re: Recursion in Macros
Date: 
Message-ID: <barmar-9256DE.20144601012006@comcast.dca.giganews.com>
In article <···············@individual.net>,
 Ulrich Hobelmann <···········@web.de> wrote:

> Hi, can Lisp macros call themselves recursively?
> 
> I tried something like `(foo ,(blarb ...)) inside a blarb macro, and get 
> some kind of error (telling me that blarb isn't a function).
> 
> I rewrote the macro to simply call a recursive function that does the 
> macro-expansion, and it works, but it doesn't seem too elegant...

It should probably be something like `(foo (blarb ,...)).  The macro 
should expand into code that makes another call to the macro.

However, remember that the macro is being expanded at compile time, so 
the termination condition of the recursion has to be something that can 
be determined from the lexical sub-expressions that it operates on.  It 
can't recurse on run-time variable values.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Ulrich Hobelmann
Subject: Re: Recursion in Macros
Date: 
Message-ID: <41s98cF1fgiaqU1@individual.net>
Barry Margolin wrote:
> In article <···············@individual.net>,
>  Ulrich Hobelmann <···········@web.de> wrote:
> 
>> Hi, can Lisp macros call themselves recursively?
>>
>> I tried something like `(foo ,(blarb ...)) inside a blarb macro, and get 
>> some kind of error (telling me that blarb isn't a function).
>>
>> I rewrote the macro to simply call a recursive function that does the 
>> macro-expansion, and it works, but it doesn't seem too elegant...
> 
> It should probably be something like `(foo (blarb ,...)).  The macro 
> should expand into code that makes another call to the macro.

Yes, thanks to you and Bruce.  That works.  Completely forgot that 
macro-expansions will automatically expand again...

> However, remember that the macro is being expanded at compile time, so 
> the termination condition of the recursion has to be something that can 
> be determined from the lexical sub-expressions that it operates on.  It 
> can't recurse on run-time variable values.

Oh sure, otherwise even the function invoked by the macro wouldn't 
terminate.

-- 
the bottom line is that a JavaSchool that won't teach C and won't teach
Scheme is not really teaching computer science, either.  --  Joel Spolsky
From: Coby Beck
Subject: Re: Recursion in Macros
Date: 
Message-ID: <zweuf.24223$AP5.18863@edtnps84>
"Ulrich Hobelmann" <···········@web.de> wrote in message 
····················@individual.net...
> Barry Margolin wrote:
>> In article <···············@individual.net>,
>
>> However, remember that the macro is being expanded at compile time, so 
>> the termination condition of the recursion has to be something that can 
>> be determined from the lexical sub-expressions that it operates on.  It 
>> can't recurse on run-time variable values.
>
> Oh sure, otherwise even the function invoked by the macro wouldn't 
> terminate.

If by "the function invoked by the macro" you mean the macro function, then 
you've got it.  If you mean the functions called in the macro-expansion then 
no, they will never run you will overflow the stack at macro-expansion time, 
usually compile time.

-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Ulrich Hobelmann
Subject: Re: Recursion in Macros
Date: 
Message-ID: <41tbs2F1fi4i4U1@individual.net>
Coby Beck wrote:
> "Ulrich Hobelmann" <···········@web.de> wrote in message 
> ····················@individual.net...
>> Barry Margolin wrote:
>>> In article <···············@individual.net>,
>>> However, remember that the macro is being expanded at compile time, so 
>>> the termination condition of the recursion has to be something that can 
>>> be determined from the lexical sub-expressions that it operates on.  It 
>>> can't recurse on run-time variable values.
>> Oh sure, otherwise even the function invoked by the macro wouldn't 
>> terminate.
> 
> If by "the function invoked by the macro" you mean the macro function, then 
> you've got it.  If you mean the functions called in the macro-expansion then 
> no, they will never run you will overflow the stack at macro-expansion time, 
> usually compile time.

No, my defmacro was just a wrapper, calling the recursive function that 
built the expanded Lisp code.  Now it's all just one macro, but it does 
the same stuff, i.e. only recur on the input given an compile-time.

-- 
the bottom line is that a JavaSchool that won't teach C and won't teach
Scheme is not really teaching computer science, either.  --  Joel Spolsky
From: Bruce Hoult
Subject: Re: Recursion in Macros
Date: 
Message-ID: <bruce-DCFD30.13013702012006@news.clear.net.nz>
In article <···············@individual.net>,
 Ulrich Hobelmann <···········@web.de> wrote:

> Hi, can Lisp macros call themselves recursively?
> 
> I tried something like `(foo ,(blarb ...)) inside a blarb macro, and get 
> some kind of error (telling me that blarb isn't a function).

The problem is the comma.  Lose it.  Then '(foo (blarb ...)) will be 
returned as the result of your macro, and CL will parse it and go "oh .. 
there's a blarb macro in here .. better expand it".

-- 
Bruce |  41.1670S | \  spoken |          -+-
Hoult | 174.8263E | /\ here.  | ----------O----------