home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / lisp / 3185 < prev    next >
Encoding:
Text File  |  1993-01-06  |  2.6 KB  |  65 lines

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