From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: Is it possible for a macro to expand to nothing? Date: Sat, 28 Nov 2009 01:06:10 +0100 Organization: Informatimago Message-ID: <87d4335x8t.fsf@galatea.local> References: <87vdh1ccra.fsf@galatea.local> <87my2dc8d7.fsf@galatea.local> <873a44dcf2.fsf@galatea.local> <87pr78b6n9.fsf@galatea.local> <87ljhwb2dx.fsf@galatea.local> <87r5rjwoag.fsf@lion.rapttech.com.au> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1259368852 16078 80.91.229.12 (28 Nov 2009 00:40:52 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 28 Nov 2009 00:40:52 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Nov 28 01:40:45 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1NEBMi-0000iX-Tn for geh-help-gnu-emacs@m.gmane.org; Sat, 28 Nov 2009 01:40:45 +0100 Original-Received: from localhost ([127.0.0.1]:58329 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NEBMi-0006Lg-7j for geh-help-gnu-emacs@m.gmane.org; Fri, 27 Nov 2009 19:40:44 -0500 Original-Path: news.stanford.edu!usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 70 Original-X-Trace: individual.net 6G+tNGZexfxnmB4pjr5OxQA0N2eMVNoL1Ix32SjebH/gmRtd8z Cancel-Lock: sha1:NDA3N2M5OGRjNjRhMThiMWQ5MGVmODBlODc5ZTE2MjQyMjE4NTEyOQ== sha1:nJgTIk9zWBOA+NmMyU94EwMfZhc= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin) Original-Xref: news.stanford.edu gnu.emacs.help:175130 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:70204 Archived-At: Tim X writes: > On the other hand, Alan's arguments also have merit. If a macro can be > useful in generating something other than a form that can be evaluated, > such as a data structure and can do so in a way that is cleaner/simpler > or just easier to understand than doing the same using functions, then > it would make sense. His examples from the C modes seem to be a case in > point. Perhaps Alan's problem with functions comes from the confusion between backquote and macros. Since backquote (and , and ,@) are often used in macros, some people believe they can be used only in macros, and that they ARE what macros are. Far from it! I don't know any language more orthogonal than lisp. Backquote can be used in a function to build a s-exp (including part of a form) as it can be used anywhere. Therefore it is really not easier to use macros to generate parts of a form than function. You should never write or use a macro to generate data. To generate data, always use functions: (defun generate-cond-clause (var predicate body) `((,predicate ,var) ,@body)) So then you can write a macro using this functio: (defmacro pcase (expr &rest clauses) (let ((var (gensym))) `(let ((,var ,expr)) (cond ,@(mapcar (lambda (clause) (generate-cond-clause var (first clause) (rest clause))) clauses))))) (macroexpand ' (pcase 42 (oddp (print 'odd) 1) (zerop (print 'zero) 0) (integerp (print 'even) 2) (identity (print 'anything) 3) (null (print 'null) nil))) --> (let ((G62061 42)) (cond ((oddp G62061) (print (quote odd)) 1) ((zerop G62061) (print (quote zero)) 0) ((integerp G62061) (print (quote even)) 2) ((identity G62061) (print (quote anything)) 3) ((null G62061) (print (quote null)) nil))) If you wanted to use macros, in addition to the complexity of having to use macroexpand to use it, you would have the difficulty of passing the parameters, since a macro gets it's parameters from the source form. In the case of a function, you have the choice to quote or not to quote the parameters, with macros they're always quoted for you. -- __Pascal Bourguignon__