News for hackers. Stuff that matters.
Re:Why Are Continuations So Confusing, and What Do They Real... by pc on Thursday March 13, 2008 @11:35PM (#136527)
His hypothetical continuations don't quite work.

  theSite = mark();
  puts "Hello, World!"
  theSite.return();
This is the first example in the post, and one which it's claimed will loop continually. Translating to Scheme (where we can actually run the pseudo-code):

  (begin
    (define theSite #f)
    (set! theSite (call/cc (lambda (c) c)))
    (print "Hello, World!") (newline)
    (theSite #f))
And we get:

  > (begin
      (define theSite #f)
      (set! theSite (call/cc (lambda (c) c)))
      (print "Hello, World!") (newline)
      (theSite #f))
  "Hello, World!"
  "Hello, World!"
  procedure application: expected procedure, given: #f; arguments were: #f
This translation to Scheme seems fair, since the definition of mark() explicitly states that it "allows you to pass information back to the original site, so that it evaluates to a different value" -- which means that the return value of mark() itself is clearly significant, and therefore that the step of "set theSite to the result of evaluating the current form" is considered part of the continuation (or "mark").

In this case, theSite gets set to #f, and the continuation is then "broken".

It might seem tedious to nitpick here, but continuations are so mysterious exactly because of these issues. mark() _can't be fixed_ without drastically changing the semantics of continuations. This is kinda non-obvious, and something that people often discover suddenly when wondering why call/cc is implemented in such a roundabout fashion.

Continuations are confusing to point where even toy examples in pedagogical blog posts are easy to mess up.

Re:Why Are Continuations So Confusing, and What Do They Real... by jcl on Friday March 14, 2008 @01:12PM (#136919)
What Scheme are you running this with? I tried your code in DrScheme, and it prints "Hello, World!" exactly once, with no error, no infinite loop. I'm not sure why, but I expect it has something to do with the semantics of "begin" (maybe a bug in DrScheme?); you can make the code work in DrScheme as you describe if you use "let" instead of "begin":

  (let ((cont #f)) 
    (set! cont (call/cc (lambda (k) k)))
    (print "Hello, world!")
    (cont #f))
You can get an infinite loop by changing where the variable is set:

  (let ((cont #f)) 
    (call/cc (lambda (k) (set! cont k)))
    (print "Hello, world!")
    (cont #f))
Re:Why Are Continuations So Confusing, and What Do They Real... by jules on Friday March 14, 2008 @12:59PM (#136908)
Mark can be fixed; your translation is wrong.