unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Kevin Ryde <user42@zip.com.au>
Subject: Re: while break and continue
Date: Thu, 05 Jun 2003 11:42:46 +1000	[thread overview]
Message-ID: <878yshe1ux.fsf@zip.com.au> (raw)
In-Reply-To: 87he79ic44.fsf@zagadka.ping.de

Marius Vollmer <mvo@zagadka.de> writes:
>
> you shouldn't use procedure->memoizing-macro (use
> define-macro instead) and that you inject literal procedures into the
> expanded code.

Ah right, yep.  New code and prospective documentation below.

The code is still pretty ugly, I suppose that's what you get for
non-local exits :-).  Maybe should have a caveat in the docs about
style, to discourage their use.

I wonder if the value parameter for break should be optional, and have
while return unspecified when not given.  That might be a reasonably
common usage.  Unless the whole thing is emulating some established
convention.

Incidentally, where would be a good place to put some tests?
syntax.test maybe, or a new file?


(define-macro (while cond . body)
  (let ((break-key    (gensym " while break-key"))
	(continue-key (gensym " while continue-key")))
    `(catch ',break-key
	    (lambda ()
	      (do ((break    (lambda (value) (throw ',break-key value)))
		   (continue (lambda ()      (throw ',continue-key))))
		  ((not (catch ',continue-key
			       (lambda ()
				 ,(if (null? body)
				      cond  ;; avoid some code if body empty
				      `(and ,cond
					    (begin
					      ,@body
					      #t))))
			       (lambda args #t))))))
	    (lambda (key value)
	      value))))



 - syntax: while cond body ...
     Run a loop executing BODY while COND is true.  COND is tested at
     the start of each iteration, so if it's `#f' the first time then
     BODY is not executed at all.  The return value is unspecified.

     Within `while' two additional bindings are provided, and can be
     used from both COND and BODY.

      - Scheme Procedure: break value
          Break out of the `while' form, and have it return the given
          VALUE.

      - Scheme Procedure: continue
          Abandon the current iteration, and continue with the next.
          Ie. go to the top of the loop, test COND again, etc.

     Each `while' provides separate `break' and `continue' procedures,
     and they operate on that `while'.  So for instance when `while'
     loops are nested the outer `break' can be used from within the
     inner loop to get all the way out.

          (while (test1)
            (let ((outer-break break))
              (while (test2)
                (if (something)
                  (outer-break #f)))))

     Note that each `break' and `continue' procedure can only be used
     within the dynamic extent of their corresponding `while'.  Once
     the `while' has been exited their behaviour is unspecified.


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


  reply	other threads:[~2003-06-05  1:42 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-30  0:00 while break and continue Kevin Ryde
2003-06-01 23:58 ` Marius Vollmer
2003-06-05  1:42   ` Kevin Ryde [this message]
2003-06-18 22:56     ` Marius Vollmer
2003-06-21 23:28       ` Kevin Ryde
2003-07-27 14:48         ` Marius Vollmer
2003-07-29  0:23           ` Kevin Ryde
2003-08-13 21:49             ` Kevin Ryde
2003-08-13  9:27               ` Matthias Koeppe
2003-08-14 23:35                 ` Kevin Ryde
2003-08-15  1:43                 ` Kevin Ryde
2003-06-06 22:31   ` Kevin Ryde
2003-06-18 22:57     ` Marius Vollmer
2003-06-20 23:56       ` Kevin Ryde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=878yshe1ux.fsf@zip.com.au \
    --to=user42@zip.com.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).