The Scheme Programming Language says
Scheme allows the continuation of any expression to be captured with the procedure
call/cc.call/ccmust be passed a procedurepof one argument.call/ccconstructs a concrete representation of the current continuation and passes it top. The continuation itself is represented by a procedurek. Each timekis applied to a value, it returns the value to the continuation of thecall/ccapplication. This value becomes, in essence, the value of the application ofcall/cc. Ifpreturns without invokingk, the value returned by the procedure becomes the value of the application ofcall/cc.
Are the two following ways of defining p equivalent, as far as being called by call/cc is concerned:
preturns without invokingk,pcallskwith its otherwise return value?
I am not sure how call/cc is defined.
Does call/cc ever directly call the continuation k, besides indirectly via p calling k?
Is it perfectly fine that both call/cc and p don't invoke continuation k?
Yes,
(call/cc (lambda (k) 1)) <=> (call/cc (lambda (k) (k 1))). You can prove this by using the continuation passing style transform.The key part is the CPS form of
call/ccis(lambda (k) (lambda (f) ((f k) k))). And the CPS forms of the two functions are(lambda (c) (lambda (k) (c 1)))and(lambda (c) (lambda (k) (k 1))). Substitute and simplify and both result in(lambda (k) (k 1)).I greatly prefer delimited continuations as they have:
This can also be proved algebraically.