Search code examples
schemeracketcontinuationscallcc

Type of a continuation in Racket and determining current continuation


What is the type of a continuation in Racket? And how to determine current continuation looking at a call/cc invocation? (e.g : Is it a correct strategy to assume that the current continuation is what follows immediately after call/cc closing bracket?)


Solution

  • A continuation is a procedure (in the sense that it's callable and returns true for procedure?), albeit a special one that does not return to the caller of the continuation.

    The value(s) you call the continuation with will become the return value(s) of the call/cc invocation that created it.

    Example:

    > (define $k #f)
    > (call-with-values (lambda () (call/cc (lambda (k)
                                              (set! $k k))))
                        (case-lambda (() "Zero values")
                                     ((x) "One value")
                                     ((x y) "Two values")
                                     ((x y z) "Three values")))
    "One value"
    > (procedure? $k)
    #t
    > ($k)
    "Zero values"
    > ($k 1)
    "One value"
    > ($k 1 2)
    "Two values"
    > ($k 1 2 3)
    "Three values"
    > ($k 1 2 3 4)
    #<case-lambda-procedure>: arity mismatch;
     the expected number of arguments does not match the given number