[Haskell-cafe] Continuations

Joel Reymont joelr1 at gmail.com
Fri Dec 23 04:49:19 EST 2005


My current setup involves threads that block on a MVar/TMVar until  
they read an event from it and then proceed. I would like to convert  
these threads into continuations whereby a continuation is saved when  
an event is requested and I can call that continuation when an even  

I had done this in Lisp before like this:

(defun/cc receive (game)
   (let/cc k
     ;; save continuation
     (setf (continuation game) k)
     ;; TODO: start a timer here

(defun/cc ask-for-blind (game amount context)
   (let ((posted nil)
         (seat nil)
         (active (car context))
         (small-blind-p (= (small-blind$ game) amount)))
     (while (and (not posted) (car active))
       (setf seat (pop active))
       ;; skip people who are waiting
       ;; for the big blind if small blind
       ;; is being posted.
       (unless (and (waiting-for-bb-p seat)
         (setf (state seat) 'sitting-out)
         (setf (current game) seat)
         (send (player seat) 'blind amount)
         (let* ((cmd (receive game)) <------- note the call to  
receive here
                (action (first cmd))
                (bet (second cmd))
                (inplay$ (inplay$ (player seat))))

How would this translate into Haskell and the Cont monad?

Alternatively, I would appreciate an example that requests, say, two  
Ints by saving a continuation each time and returns the sum.

	Thanks, Joel


More information about the Haskell-Cafe mailing list