Xref: utzoo comp.lang.lisp:619 comp.emacs:2438 Path: utzoo!mnetor!uunet!husc6!hao!ames!hc!beta!dzzr From: dzzr@beta.UUCP (Douglas J Roberts) Newsgroups: comp.lang.lisp,comp.emacs Subject: A CL iteration macro, "while". Message-ID: <13639@beta.UUCP> Date: 22 Dec 87 18:09:18 GMT Organization: Los Alamos Natl Lab, Los Alamos, N.M. Lines: 51 Keywords: Let's have a little non-flamable fun... I was poking around in the source for Stallman's Gnu Emacs the other day and stumbled across a "while" function that I kind of liked. It's not Common nor Zeta LISP, and so I'd never seen it before. We have Symbolic's big loop macro installed on all of our Common LISP machines (Suns, TI Explorers, & Symbolics), but even with it there is no clean way to do simple iteration control that I really like. (I don't like the syntax of do, dotimes, dolist, etc.) I thought it would be fun to write a CL while macro and then post it for comment, etc. I'd be curious for anybody interested enough to suggest other ways of writing it. Here 'tis. (defmacro while (test-form &rest forms) "This macro evaluates test-form, and if the result is non-nil all subsequent forms will be iteratively evaluated until test-form evaluates to nil." (prog () again (cond ( (eval test-form) (mapcar #'eval forms) (go again) )) )) Example of use: (setq *number* 0) (while (< *number* 5) (print *number*) (print "line 1") (print "line 2") (print "line 3") (setq *number* (1+ *number*)) ) -- --------------------------------------------------------------- Doug Roberts dzzr@lanl.gov ---------------------------------------------------------------