2 ms·
Not 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
by gjm11 8y ago
Not 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.