It helps me understand better, but would you have some simple code that would do that ?<br><br><br><div class="gmail_quote">2010/6/19 Paul Johnson <span dir="ltr">&lt;<a href="mailto:paul@cogito.org.uk">paul@cogito.org.uk</a>&gt;</span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div class="h5">On 19/06/10 10:36, Yves Parès wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello,<br>
<br>
I saw on the haskell wikibook that coroutines could be implemented by using continuations : <a href="http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style#Example:_coroutines" target="_blank">http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style#Example:_coroutines</a> (unhappily, the section is empty)<br>

Since I&#39;m actually learning the wonders of continuations, I just wonder : how ?<br>
   <br>
</blockquote>
<br></div></div>
Coroutines depend on the ability to suspend and resume execution.  A continuation acts as the &quot;resume point&quot; in the current function.  The &quot;callCC&quot; function in the continuation monad takes a function that expects the continuation as an argument (which is how you get access to it).  So you say something like:<br>

<br>
&gt;  yield = callCC $ \continuation -&gt; ....<br>
<br>
Then you would typically store the continuation somewhere and call some other previously stored continuation to switch contexts.<br>
<br>
Continuations can be used to pass data back into the continuation: you call the continuation with an argument, and that argument becomes the return value of the &quot;callCC&quot;.  In this case you probably just want to use ().<br>

<br>
You typically have a queue for continuations, so the new continuation goes on the back of the queue and then you call the head of the queue.  Obvious modifications for priority, simulated time, real time or whatever else you are trying to schedule.  This implies some kind of monadic state to store the queue in, so you will probably make your monad of type &quot;ContT (State Queue)&quot;<br>

<br>
If you want a thread to wait, say on a semaphore, then you have a queue of continuations in the semaphore data structure.<br>
<br>
Is this any help?<br>
<br>
Paul.<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>