Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!spool.mu.edu!snorkelwacker.mit.edu!stanford.edu!neon.Stanford.EDU!michaelg From: michaelg@neon.Stanford.EDU (Michael Greenwald) Newsgroups: comp.lang.lisp.franz Subject: Re: Lisp Macros Message-ID: <1991May16.215013.28773@neon.Stanford.EDU> Date: 16 May 91 21:50:13 GMT References: <1991May15.203735.3850@csusac.csus.edu> Organization: Computer Science Department, Stanford University, Ca , USA Lines: 62 In comp.lang.lisp.franz you write: >In article <1991May15.203735.3850@csusac.csus.edu> cs16011@athena.ecs.csus.edu writes: > Hello, I have got an exam question in my Franz Lisp class about whether > macros can be recursive or not. I have check my textbook and Franz Lisp > Interpreter's online manual here on campus but found no mentioning about > whether this is possible or not. If macros can be recursive, can anyone > please give me an example for supporting the claim? >A macro is, at some time, expanded. It's just a template for a function, an >idea, not a function per se. No, a macro >is< a function, that happens to be called by the interpreter, or compiler, on a piece of code. It returns another piece of code. There are no real restrictions on the function definition of a macro. That is, macro definition can certainly be recursive. But I don't think that's what he's asking. (I'm a little confused about the fact that this is an exam question. Is someone trying to get an answer for a question on a take home exam? Net ethics? I hope this isn't the case.) Anyway, I assume he's asking whether a macro can be >expanded< recursively. Here too the answer is yes. The result of a macro expansion is again macroexpanded, so the behavior of a macro is essentially unlimited. (As long as the expansion eventually terminates, that is). Some mildy bizarre recursive macro examples follow: (defmacro fibn-inline (n) (if (< n 2) 1 `(+ (fibn-inline ,(- n 1)) (fibn-inline ,(- n 2))))) Or (defmacro unroll (repeat-count form &rest forms) (if (zerop repeat-count) (cons 'progn forms) `(unroll ,(1- repeat-count) ,form ,form ,@forms))) >Now recursivity demands that one at some time prior to application of the >function need not know how many times the function will call itself. A macro >must know that. Hence, a macro can only be tail-recursive (i. e. an implied >for). Macros in TeX, as an example, are not recursive. The first example above isn't tail recursive. >Now if the macro could calculate its number of expansions during calculation, >there still would have to be an effective algorithm that could do that _prior_ >to expansion. >Some of my assumptions here are ...loose... I eagerly await being corrected. >Rolf Lindgren | "The opinions expressed above are >616 Bjerke Studentheim | not necessarily those of anyone" >N-0589 OSLO 5 | rolfl@hedda.uio.no