Reputation: 513
Is it possible to define a procedure f
such that it prints Exiting...
if it is the last thing to do before exiting, and prints Not done yet...
otherwise?
For examples,
(display "hello, world\n")
(f)
should give
hello, world
Exiting...
While
(f)
(display "bye, world\n")
should give
Not done yet...
bye, world
I have thought about using control operators such as shift
/ reset
, but with no success. The key difficulty seems to be that there is no way to tell if the current continuation is terminating. Any idea?
Upvotes: 1
Views: 80
Reputation: 48745
A continuation is never empty. What happens after the end is implementation specific but there is always some sort of resource deallocation and shutdown.
So imagine you have the following code which I had high hopes for:
(call/cc (lambda (end)
(define (f)
(call/cc (lambda (c)
(if (eq? c end)
(display "bye, world\n")
(display "Not done yet...")))))
(f)
(display "hello, world\n")
(f)))
Now you are not guaranteed that the continuation c
and end
can be compared even if they ae the same continuation. This has to do with the language details that upto R6RS
there were no way to compare two procedures and that we aren't really comparing procedures so the implementation might have open coded their halt
continuation such that it gets wrapped in a lambda and thus you are really comparing (eq? (lambda (v) (halt)) (lambda (v) (halt)))
and it is not guaranteed to be #t
or #f
.
Upvotes: 1