In this thread, you may post code that makes use of continuations, and I will attempt to produce code that does the same and looks reasonably similar, without using continuations.
(define (resource-manager init n)
; Limited number of resources, for instance file descriptors
(define resources (make-list-of-n-resources init n))
(define wait-queue '())
; Return a free resource, and wait until there is one to get
(define (get-resource)
(call-with-current-continuation
(lambda (k)
(if (null? resources)
(begin
; No free resource, resume caller later
(set! waiters (cons k wait-queue))
(yield-to-event-loop))
(let ((resource (car resources)))
(set! resources (cdr resources))
resource)))))
; Free a single resource and possibly let a waiting event handler run
(define (free-resource resource)
(call-with-current-continuation
(lambda (k)
(if (null? wait-queue)
(set! resources (cons resource resources))
(let (waiter (car wait-queue))
(set! waiters (cdr wait-queue))
; Suspend the caller for a while
(push-resumable-event k)
; Resume a waiting event handler
(waiter resource)))))
#t)
; Register some event handlers
(for-each
(lambda (event)
(register-event-handler event
(lambda ()
; Here, without continuations, you end up with node.js
; get-resource, free-resource and read-from-io-device all
; yield to the event loop if necessary
(let ((device ((io-manager 'get-resource))))
(display (read-from-io-device device)
((io-manager 'free-resource) device))))))
'(event-1 event-2 event-3))
Yes and it's related to Yadira, which means friend in HEBREW.
Name:
Anonymous2013-03-02 11:34
Nikita Sadkov - JEW
Name:
Anonymous2013-03-02 11:39
Nikita Sadkov - JEW Sadkov - JEW - JEW JEW
Name:
Anonymous2013-03-02 11:40
>>34
Why is that so many language developers are MtF transgender people? Audrey Tang, some chick from PyPy if I recall correctly, now Nikita "Delicate flower" Sadkova joins the ranks.
I would fill in the gaps with my own implementations but I could be oversimplifying the problem. So instead, here's the start of a thread manager...
;; A thread is a stack of functions, waiting to be called with a return value.
;; Tail calls are used to pass control between threads. The native function call stack
;; remains one layer deep.
>>41
Sorry, it doesn't run on anything. I just wrote it to show "blocking" calls on a single threaded event loop without having to resort to CPS.
While you can do much without first class continuations, trying to emulate them with closures most often forces you to write code in CPS. If you prefer that, then fine.
Name:
Anonymous2013-03-03 15:47
>>56
All non deterministic programming can be expressed as deterministic operations on lazy lists that invoke continuations upon evaluation.
Anything that can be done with continuations, can be done with tail recursion in programs that don't use the native stack.
Translating....
(define (fact-normal n)
(if (= n 0)
1
(* n (fact-normal (- n 1)))))
To.....................
(define (fact2 return n)
(if (= n 0)
(return 1)
(fact2 (lambda (ret-val)
(return (* n ret-val)))
(- n 1))))
>>59 All non deterministic programming can be expressed as deterministic operations on lazy lists that invoke continuations upon evaluation.
Right. Such is how amb is implemented on deterministic machines.
Your code example is fibs, not amb. We're still waiting.
Name:
Anonymous2013-03-03 21:43
>>60
After further inspection of amb, it has come to my attention that the interface is dependant on mutations. Hence, my religious convictions prevent me from implementing it, as it's interface is inherently flawed and harmful. Thus rather that using amb, I will explicitly use lazy lists that call functions which emulate continuations. As shown in this example...................................................................................................................................................................
I support amb because combining mutable state with continuations is awesome. Haskell's continuation monad is boring.
Name:
Anonymous2013-03-04 1:47
>>63 I support amb because combining mutable state with continuations is awesome.
How do you deal with race conditions and so? Not trolling, I really want to know.
Name:
Anonymous2013-03-04 1:49
>>59 All non deterministic programming can be expressed as deterministic operations on lazy lists that invoke continuations upon evaluation.
Does the converse hold? Can continuations be implemented in terms of non-deterministic operations?
Name:
Anonymous2013-03-04 3:56
>>65
I'm sure they can, since continuations are implemented on deterministic turning machines, but what do you have in mind?
Name:
Anonymous2013-03-04 4:18
>>66
Like almost everyone else on this board four years ago, I am trying to achieve computational enlightenment. I wish to see the true essence of computation, and I wonder whether it is purely functional, how powerful macros are, what kind of continuations there are, and so on. So far I am certain that evaluation must be strict, and that it must look and behave mostly like Lisp.
So more to the point, I am wondering whether continuations can be elegantly expressed in terms of some non-deterministic operations which would then generalize them.
Holy shit guy............................................... Are you a child?......................................................................................................................................................................................................................................
Name:
Anonymous2013-03-04 5:34
>>61
False. The only mutations in the linked example implementation are in amb-collect-thunk which is not touched by amb itself. Could you stop holding down your period key too? It makes you look like a fucking idiot (though was that the intention and HIBT?) http://bugs.call-cc.org/browser/release/4/amb/trunk/amb.scm
It is quite easy to replicate the functionality of amb without continuations, but with a load of cruft as seen in your example. I want to see amb alone.
Name:
Anonymous2013-03-04 23:23
>>70
Thanks...................................I was thrown off a little by the (require (even x))...................................................don't ask me why..........................................................................Forgot continuations.................................are..like.....................................................................................don't need....to return from....where they are...................................................................I don't like them.
Ok............I will do it.................just..give me some time....please..........