(call-with-current-continuation proc)
Proc must be a procedure of one argument. The procedure call-with-current-continuation (which is the
same as the procedure call/cc) packages the current continuation as an “escape procedure” and passes
it as an argument to proc. The escape procedure is a Scheme procedure that, if it is later called,
will abandon whatever continuation is in effect at that later time and will instead use the continuation that was in effect when the escape procedure was created. Calling the escape procedure
may cause the invocation of before and after thunks installed using dynamic-wind.
The escape procedure accepts the same number of arguments as the continuation of the original call to call-with-current-continuation. The escape procedure that is passed to proc has unlimited extent
just like any other procedure in Scheme. It may be stored in variables or data structures and may be called as many times as desired.
A common use of call-with-current-continuation is for structured, non-local exits from loops or
procedure bodies, but in fact call-with-current-continuation is useful for implementing a wide
variety of advanced control structures.
Whenever a Scheme expression is evaluated there is a continuation wanting the result of the
expression. The continuation represents an entire (default) future for the computation. Most
of the time the continuation includes actions specified by user code, as in a continuation that will
take the result, multiply it by the value stored in a local variable, add seven, and store the
result in some other variable. Normally these ubiquitous continuations are hidden behind the scenes
and programmers do not think much about them. On rare occasions, however, a programmer may need to deal with continuations explicitly. The call-with-current-continuation procedure allows Scheme
programmers to do that by creating a procedure that acts just like the current continuation.
Most programming languages incorporate one or more specialpurpose escape constructs with names like exit, return, or even goto. In 1965, however, Peter Landin [29] invented a general purpose escape
operator called the J-operator. John Reynolds [38] described a simpler but equally powerful
construct in 1972. The catch special form described by Sussman and Steele in the 1975 report on Scheme is exactly the same as Reynolds’s construct, though its name came from a less general construct in MacLisp. Several Scheme implementors noticed that the full power of the catch construct
could be provided by a procedure instead of by a special syntactic construct, and the name call-with-current-continuation was coined in 1982. This name is descriptive, but opinions differ on the merits
of such a long name, and some people use the name call/cc instead.
(values obj . . .)
Delivers all of its arguments to its continuation. The values procedure might be defined as
follows:
(define (values . things)
(call-with-current-continuation
(lambda (cont) (apply cont things))))
本文介绍Scheme语言中的call-with-current-continuation过程,该过程允许程序员创建一个行为类似于当前continuation的过程,这对于实现复杂的控制结构非常有用。此外,还提供了一个使用call-with-current-continuation实现的values过程示例。
1335

被折叠的 条评论
为什么被折叠?



