home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!julienas!corton!ilog!davis
- From: davis@passy.ilog.fr (Harley Davis)
- Newsgroups: comp.lang.lisp
- Subject: Re: macro question
- Message-ID: <DAVIS.93Jan6102702@passy.ilog.fr>
- Date: 6 Jan 93 09:27:02 GMT
- References: <1993Jan7.040552.27087@cc.umontreal.ca>
- <CONVERSE.93Jan5225205@sloth.uchicago.edu>
- Sender: news@ilog.fr
- Organization: ILOG S.A., Gentilly, France
- Lines: 51
- In-reply-to: converse@cs.uchicago.edu's message of 6 Jan 93 04:52:05 GMT
-
-
- In article <CONVERSE.93Jan5225205@sloth.uchicago.edu> converse@cs.uchicago.edu (timoshenko) writes:
-
- >I would like to define a macro which in turn defines several functions.
- >For example:
-
- >(defmacro a (x y)
- > `(defun ,x () (+ 2 2))
- > `(defun ,y () (* 2 3)))
-
- >As it stands, the macro only defines the second function, since that is the
- >returned value. How can I get both functions defined?
-
- I suppose you could do something like
-
- (defmacro a (x y)
- `(progn (defun ,x () (+ 2 2))
- (defun ,y () (* 2 3))))
-
- This is a macro that expands to an expression that, when evaluated,
- will cause both of the defun forms to be evaluated. You're right
- that the last form in the defmacro is returned as the expansion, and
- so the first defun in your version above has no effect.
-
- Why do you want to do this, though? It's likely to be confusing,
- both because it's unusual (though legal) to have defun forms that
- aren't top-level, and because this will cause functions to be defined
- without a corresponding explicit defun form in the code. I suspect
- that there is some better way to do whatever you're trying to do;
- in particular, if you are defining functions that are used only once,
- it would be better to use a lambda-expression than to automatically
- define a named function before calling it.
-
- Your solution is correct, but I don't really understand why you don't
- like it. If all functions "should" be defined by DEFUN, then you are
- severely limiting Lisp's potential for higher level syntactic
- abstractions based on macros. For example, you would also have to be
- against DEFCLASS forms which define accessor functions since these
- function definitions aren't visible directly at the top level.
-
- On the other hand, I would recommend simply as a stylistic guideline
- that a macro which defines functions be only used at the top level and
- have its name prefixed with DEF.
-
- -- Harley Davis
- --
-
- ------------------------------------------------------------------------------
- nom: Harley Davis ILOG S.A.
- net: davis@ilog.fr 2 Avenue Gallie'ni, BP 85
- tel: (33 1) 46 63 66 66 94253 Gentilly Cedex, France
-