4 ms·
Continuations also allowed me to implement python’s yield/send in 16 lines of Scheme code :-) http://billsix.github.io/bug.html#_make_generator http://billsix.
by billsix 8y ago
Continuations also allowed me to implement python’s yield/send in 16 lines of Scheme code :-)
http://billsix.github.io/bug.html#_make_generator http://billsix.github.io/bug.html#_make_generator
- hopler 8y agoYou can also implement scheme's call/cc with python's yield/send, right?
- gjm11 8y agoNot in full generality. (Well, obviously you can write a whole Scheme interpreter in Python! But you can't write something that does in Python just what call/cc does in Scheme.) Suppose you do the following: (define a-continuation (call-with-current-continuation (lambda (c) c))) The value of a-continuation at this point is a continuation object that, when called with argument x, sets a-continuation to x and returns to toplevel. So if you now do, say, (define (showfibs a b) (print a) (if (> a 1000) (a-continuation a) 1) (showfibs b (+ a b))) and (showfibs 0 1) then you'll get 011235813213455891442333776109871597 as output, the new value of a-continuation will be 1597, and you'll be back at your top-level REPL prompt. I don't think anything you can do with Python generators has such a pervasive ability to mess with control flow.
- billsix 8y agoIncorrect. In c terms, you can think of callcc as a procedure which memcpy s the stack onto the heap for later application. And when it is later applied, the heap allocated stack is memcpy ed back into the stack region of memory, destroying the current stack. Python and c#’s yield are nothing like that. It’s just syntactic sugar for iterators.
- dragonwriter 8y agoPython yield actually supports coroutines with bidirectional communication, not just unidirectional iterators; while not as full featured as callcc, these cover quite a lot of the use cases.
- billsix 8y agoAgreed regarding use cases. My scheme implementation does both yield and send, cleanly imho, because of call/cc.
- agumonkey 8y agothat said oleg kiselyov said that generators were .. as powerful as calcc. Or maybe close enough. I forgot the details