From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Kevin Ryde Newsgroups: gmane.lisp.guile.devel Subject: while break and continue Date: Fri, 30 May 2003 10:00:51 +1000 Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Message-ID: <87isrtmhfw.fsf@zip.com.au> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1054253002 5378 80.91.224.249 (30 May 2003 00:03:22 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Fri, 30 May 2003 00:03:22 +0000 (UTC) Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Fri May 30 02:03:20 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 19LXMd-0001Ob-00 for ; Fri, 30 May 2003 02:03:19 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.20) id 19LXOC-0006KI-Ad for guile-devel@m.gmane.org; Thu, 29 May 2003 20:04:56 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.20) id 19LXMS-0005Hd-Bf for guile-devel@gnu.org; Thu, 29 May 2003 20:03:08 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.20) id 19LXMP-0005Fs-Qz for guile-devel@gnu.org; Thu, 29 May 2003 20:03:06 -0400 Original-Received: from snoopy.pacific.net.au ([61.8.0.36]) by monty-python.gnu.org with esmtp (Exim 4.20) id 19LXKT-0004rG-4I for guile-devel@gnu.org; Thu, 29 May 2003 20:01:05 -0400 Original-Received: from sunny.pacific.net.au (sunny.pacific.net.au [203.2.228.40]) h4U012PB006644 for ; Fri, 30 May 2003 10:01:02 +1000 Original-Received: from wisma.pacific.net.au (wisma.pacific.net.au [210.23.129.72]) by sunny.pacific.net.au with ESMTP id h4U012Qg007522 for ; Fri, 30 May 2003 10:01:02 +1000 (EST) Original-Received: from localhost (ppp16.dyn228.pacific.net.au [203.143.228.16]) by wisma.pacific.net.au (8.12.9/8.12.9) with ESMTP id h4U00xYZ008241 for ; Fri, 30 May 2003 10:01:00 +1000 (EST) Original-Received: from gg by localhost with local (Exim 3.35 #1 (Debian)) id 19LXKF-0000na-00; Fri, 30 May 2003 10:00:51 +1000 Original-To: guile-devel@gnu.org Mail-Copies-To: never User-Agent: Gnus/5.090019 (Oort Gnus v0.19) Emacs/21.2 (gnu/linux) X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Developers list for Guile, the GNU extensibility library List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.lisp.guile.devel:2449 X-Report-Spam: http://spam.gmane.org/gmane.lisp.guile.devel:2449 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