[plt-scheme] Confusing continuation behavior (a question about)
Did this woprk in 371? I doubt it.
Procedure process calls the same procedure every time.
It does not feel the (set! impl resume-here) for this is an assignment on a
local variable of procefdure impl. while procedure process looks in a global
variable with the same name.
Things may work better when placing (define (impl return) ...) within
procedure process.
You may want a look at: http://schemecookbook.org/Cookbook/CoRoutines
Jos
----- Original Message -----
From: "Grant Rettke" <grettke at acm.org>
To: "PLT-list" <plt-scheme at list.cs.brown.edu>
Sent: Thursday, July 17, 2008 10:53 PM
Subject: [plt-scheme] Confusing continuation behavior (a question about)
> Hi,
>
> In v372 I wrote some code (pretty big language) to try to understand
> how one might utilize continuations to write processes that could
> pause and resume.
>
> It is toy code, it stops at two points, each time checking for some
> "condition" to be true, and then proceeds, finally adding two numbers.
>
> Here is the code, along with some output:
>
> (define cond1 #f)
> (define cond2 #f)
> (define (foo-process arg1 arg2)
>
> (define (process)
> (call/cc impl))
>
> (define (impl return)
> (begin
> (printf "Passed in args ~a and ~a~n" arg1 arg2)
> (let/cc resume-here (set! impl resume-here))
> (if cond1
> (printf "Condition 1 is met, going to condition 2~n")
> (return "Condition 1 not met"))
> (let/cc resume-here (set! impl resume-here))
> (if cond2
> (printf "Condition 2 is met, can finish the process~n")
> (return "Condition 2 not met"))
> (printf "The process has finished with values ~a and ~a~n" arg1
> arg2)
> (let ((result (+ arg1 arg2)))
> (let/cc resume-here (set! impl resume-here))
> (return result))))
>
> process)
>
> (define a-process (foo-process 13 29))
> (define b-process (foo-process 665 1))
> (a-process)
> (set! cond1 #t)
> (a-process)
> (set! cond2 #t)
> (a-process)
> (b-process)
>
> ; Passed in args 13 and 29
> ; "Condition 1 not met~n"
> ; Condition 1 is met, going to condition 2
> ; "Condition 2 not met~n"
> ; Condition 2 is met, can finish the process
> ; The process has finished with values 13 and 29
> ; 42
>
> I just cut it over to 4.0.2 with #lang scheme, and I get a behavior I
> don't understand, it basically enters an infinite loop because the
> execution of the program never reaches the point where cond2 gets set
> to #t. Instead, it just loops forever like this (I debugged it):
>
> 1 -> 2 -> 3 -> 1 -> 2 -> 3 -> ...
>
> 1. (a-process)
> 2. (set! cond1 #t)
> 3. (a-process)
> 4. (set! cond2 #t)
>
> I have *just* started digging into this, but let/cc doesn't seem to
> have changed... more later.
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme