guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: while break and continue


From: Kevin Ryde
Subject: Re: while break and continue
Date: Thu, 05 Jun 2003 11:42:46 +1000
User-agent: Gnus/5.090019 (Oort Gnus v0.19) Emacs/21.2 (gnu/linux)

Marius Vollmer <address@hidden> 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.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]