From: Simon Brooke
Subject: Looping constructs (was: Comparison: Beta - Lisp)
Date: 
Message-ID: <CwJuu7.3un@rheged.dircon.co.uk>
In article <············@sytex.com> ····@sytex.com (Scott McLoughlin) writes:

	(** A lot of sensible thoughts about why LISP is not used in
		the trenches excised) 

   3. Nasty looping constructs.  Hard to hack DO looking at the
   manual.  LABELS and LETREC are elegant, but complicated. DOTIMES and
   DOLIST are a step in the right direction. There should have been
   a CL standard WHILE/UNTIL for "bootstrapping" nincompoops (sp?).
   LOOP is newer and not so widely used and/or written about. Otherwise
   LOOP is fine.  Of _course_ Lisp has superior looping/iteration 
   constructs, but it takes new comers a few months to realize this.

	(** More sensible thoughts excised)

Cambridge LisP has the beautiful construct

	(loop 
		forms 
		(until <condition> value) 
		forms
		(while <condition> value)
		forms)

where there can be an arbitrary number of forms, including an
arbitrary number of _until_ forms and an arbitrary number of _while_
forms in any order. The construct loops evaluating each of the forms
in turn until it comes to an _until_ form whose condition evaluates to
true, when it returns the value element of that form or nil if there
was none; or until it comes to a _while_ form whose condition
evaluates to false, in which case it returns the value element of that
form or nil if there was none.

In use it's exceedingly clear and flexible. I think this is
implemented as a (pretty cunning) macro which expands into a _prog_
wrapped around a _cond_; it might be worth spending a quiet winter's
evening reinventing it (unless someone posts the code to this
newsgroup).








-- 
    .::;====r==\              ·····@rheged.dircon.co.uk (Simon Brooke)
   /  /____||___\____         
  //==\-   ||-  |  /__\(      MS Windows IS an operating environment.
 //____\___||___|_//  \|:     C++ IS an object oriented programming language. 
   \__/ ~~~~~~~~~~ \__/       Citroen 2cv6 IS a four door family saloon.
From: David Gadbois
Subject: Re: Looping constructs
Date: 
Message-ID: <35tdfu$u8q@plucky.cs.utexas.edu>
Simon Brooke <·····@rheged.dircon.co.uk> wrote:
>Cambridge LisP has the beautiful construct
>
>	(loop 
>		forms 
>		(until <condition> value) 
>		forms
>		(while <condition> value)
>		forms)
>
>[...] it might be worth spending a quiet winter's
>evening reinventing it

It's a no-brainer:

(defmacro cambridge-loop (&body forms)
  (let ((block (make-symbol "BLOCK"))
	(start (make-symbol "START")))
    `(block ,block
       (macrolet ((until (condition &optional value)
		    `(when ,condition (return-from ,',block ,value)))
		  (while (condition &optional value)
		    `(unless ,condition (return-from ,',block ,value))))
	 (tagbody
	   ,start
	   (progn ,@forms)
	   (go ,start))))))

--David "Will write macros for food" Gadbois