Robert_Liu
Robert_Liu

Reputation: 59

A tricky question in scheme continuation definition

R5rs says

The continuation represents an entire (default) future for the computation".

So basically in the following code:

(define x (call/cc (lambda (c) c)))
(display "hello\n")
(display "world\n")
(x 4)
(display x)

I tried several implementations, all of them output

hello
world
4

It seems in this example the continuation captured by call/cc limited its scope for the first top level expression only. That likes (define x ?).

I though based on r5rs, when (x 4) is executed, execution will jump back to the beginning definition form and finish the assignment. Then it would continue to run the subsequent two display expressions and run (x 4) which would report an error since x will no longer be a procedure.

Upvotes: 4

Views: 135

Answers (1)

Peter Winton
Peter Winton

Reputation: 796

Your expectations are correct. The problem is that the REPL executes each expression separately, as if you had pressed ENTER between each one. If you wrap them in a begin, it works as you expect.

$ scheme
Chez Scheme Version 9.5.8
Copyright 1984-2022 Cisco Systems, Inc.

> (begin
   (define x (call/cc (lambda (c) c)))
   (display "hello\n")
   (display "world\n")
   (x 4)
   (display x))
hello
world
hello
world
Exception: attempt to apply non-procedure 4
Type (debug) to enter the debugger.

Upvotes: 4

Related Questions