unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* while break and continue
@ 2003-05-30  0:00 Kevin Ryde
  2003-06-01 23:58 ` Marius Vollmer
  0 siblings, 1 reply; 14+ messages in thread
From: Kevin Ryde @ 2003-05-30  0:00 UTC (permalink / 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


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2003-08-15  1:43 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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).