Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!husc6!yale!Ram-Ashwin From: Ram-Ashwin@cs.yale.edu (Ashwin Ram) Newsgroups: comp.emacs Subject: Re: defmacro vs. defun Message-ID: <33735@yale-celray.yale.UUCP> Date: 18 Jul 88 01:34:05 GMT References: <4300016@m.cs.uiuc.edu> Sender: root@yale.UUCP Reply-To: Ram-Ashwin@cs.yale.edu (Ashwin Ram) Organization: Computer Science, Yale University, New Haven, CT 06520-2158 Lines: 40 In-reply-to: liberte@m.cs.uiuc.edu In article <4300016@m.cs.uiuc.edu>, liberte@m writes: > [ Question: How much is really saved by defining a macro instead of a > [ function? The macros that caused the trouble were caar, cadr, cdar, etc. > [ Also, for these simple macros, how much MORE would be saved by just > [ writing out (car (car ...) ) instead of caar? > > If not byte-compiled, a macro call must be expanded every time > it is seen. This will definitely take longer since the > macro call is essentially a call to the macro as if it were a function > followed by evaluation of the result. If byte-compiled, there is no > penelty since the expansion is done (if it is known as a macro) at > compile time. This is correct, but I think Tom's question was really, why use macros at all instead of either writing out the code, or using a function instead? Using the (caar ...) macro is exactly the same as (car (car ...)) in terms of efficiency once the macro has been expanded. In EL, this happens during byte-compilation, but it could, and in many other Lisps it does, happen at definition time, even in interpreted code. Given this, the main reasons for using macros are: - to reduce code clutter and improve readability. E.g., (caar ...) is more readable than (car (car ...)). This gets more obvious for bigger macros. - to save the time required for a function call. E.g., (caar ...) defined as a macro is more efficient than (caar ...) defined as a function, because in the latter case you have to call and return from the caar function. - to introduce new special forms. Perhaps there should be a way to declare autoload's for macros, such that the byte-compiler would do the autoload before compilation, just like eval does before evaluation? -- Ashwin. ARPA: Ram-Ashwin@cs.yale.edu UUCP: {decvax,ucbvax,harvard,cmcl2,...}!yale!Ram-Ashwin BITNET: Ram@yalecs