[plt-scheme] while and return macro?

From: YC (yinso.chen at gmail.com)
Date: Sun Mar 2 03:24:23 EST 2008

Hi all -

what's the way to implement a return (or break) control from a infinite
while loop (i.e. (while true ...))?

A while loop with a test condition is straight forward, but I cannot figure
out how to return from an infinite loop. My thought is to introduce a hidden
conditional, and then having the inner return macro to set the condition to
be true, but having the two macros being separate basically means the return
macro's hidden variable is different from the hidden variable in while loop,
so obviously my approach is incorrect.

Below is what I have.  Thanks in advance for suggestions,
yc

(define-syntax (return stx)
  (syntax-case stx ()
    ((_ exp)
     #'(begin
         (set! __ret__ #t)
         exp))))

(define-syntax (while stx)
  (syntax-case stx (true)
    ((_ test exp ...)
     #'
         (let loop ((__ret__ #f))
         (cond (test exp ... (loop __ret__)))))
    ((_ true exp ...) ;;; this is *incorrect*...
     #'(_ (= __ret__ #f) exp ...))))

(let ((i 0))
    (while true
           (display i) (newline)
           (if (< i 10)
               (set! i (+ i 1))
               (return i))))
;; => set!: cannot set undefined identifier: __ret__
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080302/503191bf/attachment.html>

Posted on the users mailing list.