简体   繁体   English

只能用lambdas和闭包来实现call-with-current-continuation?

[英]Can call-with-current-continuation be implemented only with lambdas and closures?

Does anyone know if call/cc can be implemented with just lambdas and closures? 有没有人知道call/cc是否只能用lambdas和闭包实现?

It seems that call/cc interrupts the program's flow (like an exception) but lambdas and closures can't do that. 似乎call/cc中断了程序的流程(就像异常一样),但lambdas和closures不能这样做。 Therefore I think call/cc can't be implemented via lambdas and closures. 因此我认为call/cc不能通过lambdas和closures实现。

Any more ideas? 还有什么想法吗?

The question is not particularly clear, since what exactly does "implemented with just lambdas and closures" mean? 问题并不是特别清楚,因为“只用lambdas和闭包实现”究竟是什么意思?

In any case, continuations can be used in any language with closures by manually writing in continuation passing style . 在任何情况下,continuation都可以在任何语言中使用闭包,方法是以连续传递方式手动编写。 Then automatic translation into this form can be implemented by extending the compiler, which Lisps typically allow on user level through macros. 然后可以通过扩展编译器来实现自动转换为此表单,Lisps通常允许在用户级别通过宏。 For example see cl-cont , a library implementing continuations for Common Lisp, which is a language that doesn't have them built in. 例如,请参阅cl-cont ,这是一个实现Common Lisp的延续的库,它是一种没有内置它们的语言。

Efficient pervasive continuations like in Scheme are likely to be implemented on a lower level directly dealing with the program stack, but this is not a requirement, just an optimization. 如Scheme中的高效普遍延续可能会在直接处理程序堆栈的较低级别上实现,但这不是一个要求,只是一个优化。

In Scheme you can implement call/cc using lambdas when converting to continuation passing style (CPS). 在Scheme中,您可以在转换为延续传递样式(CPS)时使用lambdas实现call/cc When converting into CPS, every occurrence of call/cc can be replaced with the following equivalent: 转换为CPS时,每次出现的call/cc都可以替换为以下等价物:

(lambda (f k) (f (lambda (v k0) (k v)) k))

where k is the continuation to be saved, and (lambda (v k0) (kv)) is the escape procedure that restores this continuation (whatever continuation k0 that is active when it is called, is discarded). 其中k是要保存的连续符,并且(lambda (v k0) (kv))是恢复此连续的转义过程(无论在被调用时有效的连续k0被丢弃)。

So, to answer your question for Scheme: yes, it can be done. 所以,回答你对Scheme的问题:是的,可以做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM