unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Kevin Ryde <user42@zip.com.au>
Subject: while break and continue
Date: Fri, 30 May 2003 10:00:51 +1000	[thread overview]
Message-ID: <87isrtmhfw.fsf@zip.com.au> (raw)

I was going to add some words to the manual about break and continue
in a while loop, but noticed continue doesn't do what I might have
thought.  For instance,

	(define n 0)
	(while (begin
	         (format #t "test ~a\n" n)
	         (< n 2))
	       (format #t "body ~a\n" n)
	       (set! n (1+ n))
	       (if #t
	           (continue))
	       (format #t "unreachable ~a\n" n))

prints

	test 0
	body 0
	test 1
	body 1
	test 2
	unreachable 2
	test 2
	unreachable 2
	test 2

whereas I might have hoped "unreachable" would indeed have been
unreachable, and the test wouldn't be evaluated again once false (at
n=2).

Is a throw the best way for continue to extricate itself from the
body?  I take it that's the intention.  If so perhaps something like
the following (only tested a bit),

(define while
  (procedure->memoizing-macro
   (lambda (expr env)
     (let* ((break-key     (gensym " while break-key"))
	    (continue-key  (gensym " while continue-key"))
	    (break-proc    (lambda (value) (throw break-key value)))
	    (continue-proc (lambda ()      (throw continue-key))))
       `(catch ',break-key
	       (lambda ()
		 (do ()
		     ((not ,(cadr expr)))
		   (let ((break    ,break-proc)
			 (continue ,continue-proc))
		     (catch ',continue-key
			    (lambda ()
			      ,@(cddr expr))
			    noop))))
	       (lambda args (cadr args)))))))

Each while loop gets its own break and continue keys and procedures,
to allow those procedures to be used from inner nested loops.  The new
bindings are only for the body forms, maybe they should be available
to the condition expression too.

Not really a thing of beauty, and probably not fast.  A good reason
not to use unstructured stuff like "continue" I guess :-).


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


             reply	other threads:[~2003-05-30  0:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-30  0:00 Kevin Ryde [this message]
2003-06-01 23:58 ` while break and continue Marius Vollmer
2003-06-05  1:42   ` Kevin Ryde
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=87isrtmhfw.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).