Help with dynamic-wind and call/cc

Posted by josh on Stack Overflow See other posts from Stack Overflow or by josh
Published on 2010-06-13T11:13:37Z Indexed on 2010/06/13 12:32 UTC
Read the original article Hit count: 469

Filed under:
|

I am having some trouble understanding the behavior of the following Scheme program:

(define c
  (dynamic-wind
    (lambda () (display 'IN)(newline))
    (lambda () (call/cc (lambda (k)
                     (display 'X)(newline)
                     k)))
    (lambda () (display 'OUT)(newline))))

As I understand, c will be bound to the continution created right before "(display 'X)".

But using c seems to modify itself! The define above prints (as I expected) IN, X and OUT:

IN
X
OUT

And it is a procedure:

#;2> c
#<procedure (a9869 . results1678)>

Now, I would expect that when it is called again, X would be printed, and it is not!

#;3> (c)
IN
OUT

And now c is not a procedure anymore, and a second invokation of c won't work!

#;4> c    ;; the REPL doesn't answer this, so there are no values returned
#;5> (c)

Error: call of non-procedure: #<unspecified>

        Call history:

        <syntax>            (c)
        <eval>              (c)    <--

I was expecting that each invokation to (c) would do the same thing -- print IN, X, and OUT. What am I missing?

© Stack Overflow or respective owner

Related posts about Scheme

Related posts about continuations