Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I envy the languages with continuations. That makes the implementation of backtracking so much easier.


Not only backtracking but also other control flow structures: https://curiosity-driven.org/continuations#flow-structures


Continuations also allowed me to implement python’s yield/send in 16 lines of Scheme code :-)

http://billsix.github.io/bug.html#_make_generator


You can also implement scheme's call/cc with python's yield/send, right?


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.


Incorrect. 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.


Python 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.


Agreed regarding use cases. My scheme implementation does both yield and send, cleanly imho, because of call/cc.


that said oleg kiselyov said that generators were .. as powerful as calcc. Or maybe close enough. I forgot the details




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: