Path: utzoo!utgpu!water!watmath!clyde!rutgers!mit-eddie!bloom-beacon!think!ames!pasteur!ucbvax!sdcsvax!ucsdhub!esosun!seismo!uunet!mcvax!ukc!its63b!aiva!jeff From: jeff@aiva.ed.ac.uk (Jeff Dalton) Newsgroups: comp.lang.lisp Subject: Re: A CL iteration macro, "while". Keywords: LOOP WHILE UNTIL ITERATION MACROS Message-ID: <228@aiva.ed.ac.uk> Date: 18 Jan 88 18:15:41 GMT References: <13639@beta.UUCP> <3503@whuts.UUCP> <464@dcl-csvax.comp.lancs.ac.uk> Reply-To: jeff@uk.ac.ed.aiva (Jeff Dalton) Organization: Dept. of AI, Univ. of Edinburgh, UK Lines: 54 In article <464@dcl-csvax.comp.lancs.ac.uk> simon@comp.lancs.ac.uk (Simon Brooke) writes: >Yes, that's nice; but there's a nicer. How about this, which is Arthur >Norman's loop macro: > (loop > > (until ) > > (while ) > ) In ordinary Common Lisp, you can do something similar: (loop
... (when ... (return )) ... (unless ... (return )) ) >The 'while' and 'until' conditions can appear any number of times, >(including zero) anywhere in the body of the loop. The flexibility of this >is wonderful - and I'd love to see the source code for it! I think it's a special form (not a macro) in Acorn's Lisp for the BBC micro, so it's not as portable as we might like. But it's fairly easy to code. You could process loop bodies, as in Richard O'Keefe's message about REPEAT, or use MACROLET thus: ;;; Arthur Norman's loop macro (defmacro loop (&body body) (let ((label (gensym))) `(macrolet ((while (test &body exit-forms) `(if (not ,test) (return (progn ,@exit-forms)))) (until (test &body exit-forms) `(if ,test (return (progn ,@exit-forms))))) (block nil (tagbody ,label (progn . ,body) (go ,label)))))) Unfortunately, the macrolet provides somewhat excessive generality. (The WHILEs and UNTILs don't have to be at the top level of the loop.) Jeff Dalton, JANET: J.Dalton@uk.ac.ed AI Applications Institute, ARPA: J.Dalton%uk.ac.ed@nss.cs.ucl.ac.uk Edinburgh University. UUCP: ...!ukc!ed.ac.uk!J.Dalton