Subject: Re: Tutorial on writing Macros?
From: (Rob Warnock)
Date: 27 Oct 2000 11:34:57 GMT
Newsgroups: comp.lang.scheme
Message-ID: <8tbp91$3bftp$>
Friedrich Dominicus  <> 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)
		   (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
	      '(let ((in '(a b c d))
		     (out '()))
	         (dolist (i in out)
	           (push i out)))))
	  (((in) (#%quote (a b c d))) ((out) (#%quote ())))
	  (for-each (#%lambda (i) (#%set! out (cons i out)) out) in)



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