From: Kardan Kaveh
Subject: macro question
Date: 
Message-ID: <1993Jan7.040552.27087@cc.umontreal.ca>
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?

Thanks.

-- 
------------
Kaveh Kardan                                       ·····@taarna.UUCP
Systeme Taarna                                     ·······@eole.umontreal.qc.ca
Montreal Quebec Canada                             (514)844-8448

From: timoshenko
Subject: Re: macro question
Date: 
Message-ID: <CONVERSE.93Jan5225205@sloth.uchicago.edu>
In article <·····················@cc.umontreal.ca> ·······@ERE.UMontreal.CA (Kardan Kaveh) 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.

	--Tim Converse

--
-------------------------------------------------------------------------------
Tim Converse  U. of Chicago CS Dept.  ········@cs.uchicago.edu  (312) 702-8584
From: Harley Davis
Subject: Re: macro question
Date: 
Message-ID: <DAVIS.93Jan6102702@passy.ilog.fr>
In article <·····················@sloth.uchicago.edu> ········@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: ·····@ilog.fr			2 Avenue Gallie'ni, BP 85
tel: (33 1) 46 63 66 66			94253 Gentilly Cedex, France
From: timoshenko
Subject: Re: macro question
Date: 
Message-ID: <CONVERSE.93Jan7001530@sloth.uchicago.edu>
>In article <·····················@sloth.uchicago.edu>
········@cs.uchicago.edu (timoshenko) writes:

>>I suppose you could do something like

>>(defmacro a (x y)
	 `(progn (defun ,x () (+ 2 2))
		 (defun ,y () (* 2 3))))
[..]
>>Why do you want to do this, though?  It's likely to be confusing, 

In article <··················@passy.ilog.fr> ·····@passy.ilog.fr (Harley Davis) writes:
>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.

	Sure, you're right that DEFCLASS and DEFSTRUCT and the like
have the useful side-effect of defining new functions, but those have
the advantage that they are part of the language already.  And, in
fact, such side-effects _are_ confusing (though in DEFCLASS etc. the
confusion is probably worth it).  I remember a confused posting a long
time ago from someone working through an AI programming book (AIP2?)
who wanted to know where in the code MAKE-NODE and NODE-SLOT were
defined.  And the answer was (in a sense) "Nowhere." since they resulted
from a defstruct.  If you know the forms that have this sort of effect
you won't be similarly confused, but you should probably have a pretty
good reason before you use macros to create new ones.  And I wondered 
whether the original poster's problem really needed to extend the
language that far, or whether he was taking that approach because he
was confused about something else.

	--Tim Converse
--
-------------------------------------------------------------------------------
Tim Converse  U. of Chicago CS Dept.  ········@cs.uchicago.edu  (312) 702-8584
From: Kardan Kaveh
Subject: Re: macro question
Date: 
Message-ID: <1993Jan7.163546.27947@cc.umontreal.ca>
In article <·····················@sloth.uchicago.edu> ········@cs.uchicago.edu (timoshenko) writes:
>>In article <·····················@sloth.uchicago.edu>
>········@cs.uchicago.edu (timoshenko) writes:
>
>>>I suppose you could do something like
>
>>>(defmacro a (x y)
>	 `(progn (defun ,x () (+ 2 2))
>		 (defun ,y () (* 2 3))))
>[..]
>>>Why do you want to do this, though?  It's likely to be confusing, 
>
>In article <··················@passy.ilog.fr> ·····@passy.ilog.fr (Harley Davis) writes:
>>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.
>
>	Sure, you're right that DEFCLASS and DEFSTRUCT and the like
>have the useful side-effect of defining new functions, but those have
>the advantage that they are part of the language already.
[...] 
>you won't be similarly confused, but you should probably have a pretty
>good reason before you use macros to create new ones.  And I wondered 
>whether the original poster's problem really needed to extend the
>language that far, or whether he was taking that approach because he
>was confused about something else.
>

I wish to define a macro for defining classes, which also defines slot
accessors that evaluate the slots.  In this manner, I can place '(+ 2 2)
in a slot, and get back 4 when I access the slot.

It seems to me like a reasonable motivation for the type of macro described
above, but if it isn't, I would be interested in a more elegant solution.

Kaveh


-- 
------------
Kaveh Kardan                                       ·····@taarna.UUCP
Systeme Taarna                                     ·······@eole.umontreal.qc.ca
Montreal Quebec Canada                             (514)844-8448
From: Barry Margolin
Subject: Re: macro question
Date: 
Message-ID: <1ihqrhINNg65@early-bird.think.com>
In article <·····················@sloth.uchicago.edu> ········@cs.uchicago.edu (timoshenko) writes:
>	Sure, you're right that DEFCLASS and DEFSTRUCT and the like
>have the useful side-effect of defining new functions, but those have
>the advantage that they are part of the language already.

Once upon a time they weren't part of the language.  If programmers had
avoided writing macros that define new functions, they never would have
been written, and then they never would have become popular enough to be
included in the standard language.  Lisp's powerful macro capability is
what has enabled it to evolve so well over the years; new definition and
control structures can be added as easily as procedures can.

Function-defining macros are a well-known, useful technique for procedural
abstraction.  It allows you to write a single macro that incorporates the
structural elements common to a class of functions that should be defined;
the macro in the original post was obviously contrived and didn't
demonstrate this utility.

-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar