Subject: Re: Tutorial on writing Macros?
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 27 Oct 2000 11:34:57 GMT
Newsgroups: comp.lang.scheme
Message-ID: <8tbp91$3bftp$1@fido.engr.sgi.com>
Friedrich Dominicus  <frido@q-software-solutions.com.NO-spam> wrote:
+---------------
| One question still remains. How do use expand-defmacro?  
+---------------

[Assuming DrScheme/MzScheme:]

Give it a form (if typing it in, you'll probably need to quote it) and
it'll give you back the form with all the macros expanded. For example,
here are a couple of Common Lisp compatibility hacks I tend to use:

	> (defmacro push (item place)
	  `(begin (set! ,place (cons ,item ,place)) ,place))
	> (defmacro bind-list (ls expr . body) ;subset of "destructuring-bind"
	    `(apply (lambda ,ls ,@body) ,expr))
	> (defmacro dolist (parms . body)
	    (bind-list (var ls . result) parms
	      (if (null? result)
	        `(for-each (lambda (,var) ,@body) ,ls)
	        `(begin
		   (for-each (lambda (,var) ,@body) ,ls)
		   ,(car result)))))
	> 

Now suppose you want to see how some usage expands:

	> (expand-defmacro
	    '(dolist (i '(a b c d))
	       (print i)))
	(for-each (#%lambda (i) (print i)) (#%quote (a b c d)))
	> (pretty-print
	    (expand-defmacro
	      '(let ((in '(a b c d))
		     (out '()))
	         (dolist (i in out)
	           (push i out)))))
	(#%let-values
	  (((in) (#%quote (a b c d))) ((out) (#%quote ())))
	  (for-each (#%lambda (i) (#%set! out (cons i out)) out) in)
	  out)
	> 

See?


-Rob

-----
Rob Warnock, 31-2-510		rpw3@sgi.com
Network Engineering		http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		PP-ASEL-IA
Mountain View, CA  94043