Subject: Re: dotimes dolist
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 2000/08/29
Newsgroups: comp.lang.scheme
Message-ID: <8og5op$lq794$1@fido.engr.sgi.com>
David Pirotte  <david@altosw.be> wrote:
+---------------
| is there a good macro implementation for
| 	dotimes
| 	dolist
+---------------

MzScheme, SCM, Elk, and many other implementations either already 
provide CL-style "defmacro" or else "defmacro" can be defined using
whatever low-level macro facility there is, e.g., "define-macro":

	(define-macro defmacro          ; same macros for SCM/MzScheme/CL
	  (lambda (name args . body)
	    `(define-macro ,name (lambda ,args ,@body))))

Given such, I personally prefer doing CL-style stuff like "dotimes"
and "dolist" using "defmacro". [O.k., so I'm a heretic!] Here are
the simplified versions I use:

	;; a simplified CL-style "destructuring-bind":
	(defmacro bind-list (ls expr . body)
	  `(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)))))

	(defmacro dotimes (parms . body)
	  (bind-list (var count . result) parms
	    (let ((limit (gensym)))
	      `(let ((,limit ,count))
		 (do ((,var 0 (+ ,var 1)))
		     ((>= ,var ,limit) ,@result)
		   ,@body)))))


-Rob

-----
Rob Warnock, 41L-955		rpw3@sgi.com
Applied Networking		http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		PP-ASEL-IA
Mountain View, CA  94043