<br>Thanks for demonstrating the use of continuations - it's quite cool and I need to grok continuation more. Thanks! <br><br><div class="gmail_quote">On Sun, Mar 2, 2008 at 2:01 AM, Chongkai Zhu <<a href="mailto:czhu@cs.utah.edu">czhu@cs.utah.edu</a>> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Or if you don't want the explicit "break/return" in the macro:<br>
<br>
(define-syntax (while stx)<br>
(syntax-case stx ()<br>
((while cond body ...)<br>
#`(let/ec #,(datum->syntax-object #'while 'return)<br>
<div class="Ih2E3d"> (let lp ()<br>
(when cond<br>
body ...<br>
(lp)))))))<br>
<br>
<br>
</div> Happy Texas Independence Day!<br>
<font color="#888888"><br>
Chongkai<br>
</font><div><div></div><div class="Wj3C7c"><br>
<br>
<br>
Chongkai Zhu wrote:<br>
> First, you are in a functional programming language. So please stop<br>
> thinking procedurely.<br>
><br>
> Most people will use first class continuation to implement break control:<br>
><br>
> (define-syntax while<br>
> (syntax-rules ()<br>
> ((_ break cond body ...)<br>
> (let/ec break<br>
> (let lp ()<br>
> (when cond<br>
> body ...<br>
> (lp)))))))<br>
><br>
> (let ((i 0))<br>
> (while return<br>
> true<br>
> (display i) (newline)<br>
> (if (< i 10)<br>
> (set! i (+ i 1))<br>
> (return i))))<br>
><br>
><br>
><br>
><br>
> YC wrote:<br>
>> Hi all -<br>
>><br>
>> what's the way to implement a return (or break) control from a<br>
>> infinite while loop (i.e. (while true ...))?<br>
>><br>
>> A while loop with a test condition is straight forward, but I cannot<br>
>> figure out how to return from an infinite loop. My thought is to<br>
>> introduce a hidden conditional, and then having the inner return<br>
>> macro to set the condition to be true, but having the two macros<br>
>> being separate basically means the return macro's hidden variable is<br>
>> different from the hidden variable in while loop, so obviously my<br>
>> approach is incorrect.<br>
>><br>
>> Below is what I have. Thanks in advance for suggestions,<br>
>> yc<br>
>><br>
>> (define-syntax (return stx)<br>
>> (syntax-case stx ()<br>
>> ((_ exp)<br>
>> #'(begin<br>
>> (set! __ret__ #t)<br>
>> exp))))<br>
>><br>
>> (define-syntax (while stx)<br>
>> (syntax-case stx (true)<br>
>> ((_ test exp ...)<br>
>> #'<br>
>> (let loop ((__ret__ #f))<br>
>> (cond (test exp ... (loop __ret__)))))<br>
>> ((_ true exp ...) ;;; this is *incorrect*...<br>
>> #'(_ (= __ret__ #f) exp ...))))<br>
>><br>
>> (let ((i 0))<br>
>> (while true<br>
>> (display i) (newline)<br>
>> (if (< i 10)<br>
>> (set! i (+ i 1))<br>
>> (return i))))<br>
>> ;; => set!: cannot set undefined identifier: __ret__<br>
>><br>
>><br>
>><br>
>><br>
>> ------------------------------------------------------------------------<br>
>><br>
>> _________________________________________________<br>
>> For list-related administrative tasks:<br>
>> <a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme" target="_blank">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br>
> _________________________________________________<br>
> For list-related administrative tasks:<br>
> <a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme" target="_blank">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br>
</div></div></blockquote></div><br>