Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 437
Robert_Liu Avatar asked Oct 26 '25 02:10

Robert_Liu


1 Answers

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.
like image 101
Peter Winton Avatar answered Oct 28 '25 04:10

Peter Winton