[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] scheduling and coroutines
From: |
Tomtom |
Subject: |
Re: [Chicken-users] scheduling and coroutines |
Date: |
Sat, 26 Feb 2011 13:50:10 +0100 |
User-agent: |
Sup/0.11 |
Hi Peter
> Welcome. I hope you're enjoying Scheme so far :)
Definitely. I've always been amazed by the beauty and expressiveness of
functional programming.
> Yeah, you can use multiple value returns. Just change this in the scheduler:
>
> (let ((cont (call/cc (queue-remove! q))))
> ...)
>
> to this:
>
> (let-values (((cont sleep-duration) (call/cc (queue-remove! q))))
> ...)
>
> or the slightly shorter:
>
> (receive (cont sleep-duration) (call/cc (queue-remove! q))
> ...)
>
> Then when the coroutine yields to the scheduler, it can just call
> cont with more than one argument.
Sounds promising ! But I must miss something. Let's say I change my scheduler
to this, following your advice:
(define scheduler
(lambda ()
(let loop ()
(if (not (queue-empty? q))
(receive
(cont time) (call/cc (queue-remove! q))
(printf "sleep:~A~%" time)
(if
(not (unspecified? cont))
(queue-add! q cont)))
(loop)))))
Now, how should I change the code in the routine ?
If I don't change anything, obviously it won't work:
(define (routine cont)
(let loop ((n 10))
(set! cont (call/cc cont))
(if (> n 0)
(loop (- n 1)))))
Error: bad argument count - received 1 but expected 2: #<procedure (? cont
time)>
Buf if I change the routine by adding one argument to call/cc
(define (routine cont)
(let loop ((n 10))
(set! cont (call/cc cont n))
(if (> n 0)
(loop (- n 1)))))
Error: bad argument count - received 2 but expected 1: #<procedure
(call-with-current-continuation proc1295)>
Well, I guess it's normal too, as call/cc is taking only one argument - so how
am I supposed to pass this second argument ?
cheers,
Tom