可以“如果”使用“call/cc”来实现?
我被告知“call/cc”可用于实现任意控制流构造,因此我尝试使用“call/cc”实现所有此类构造,但我遇到了麻烦。假设我没有“if”,我将如何使用“define-syntax”和“call/cc”来实现它?有可能还是我被误导了?我知道如何使用“call/cc”实现无条件跳转,但在机器级别,条件执行是通过分支指令执行的,分支指令的执行取决于处理器的状态位。如果没有这种类型的构造,我不知道如何做到这一点。
I've been told that "call/cc" can be used to implement arbitrary control flow constructs so I'm trying to implement all such constructs using "call/cc" but I'm having trouble. Assuming I didn't have "if" how would I implement it using "define-syntax" and "call/cc"? Is it possible or have I been misled? I know how to implement an unconditional jump using "call/cc" but at the machine level conditional execution is performed with branching instructions whose execution depends on the status bits of the processor. Without constructs of this type I don't see how it can be done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能——你必须有某种方法来测试事物并根据它们是真是假采取行动。不过,您可以通过布尔值的一些函数表示来接近。例如,使用常见的教堂编码:
现在您可以将测试(返回这些编码布尔值之一)视为接受“成功”延续和“失败”延续的函数,并使用它来继续:
如果您想要尝试这个,你需要考虑这样一个事实:Scheme 不是一种惰性语言,所以你需要把事情搞清楚。例如:(
但您仍然需要修改现有谓词等以返回这些布尔值。)
无论哪种方式,
call/cc
本身并没有真正做任何相关的事情......You can't -- you have to have some way to test things and act on whether they're true or false. You could get close though, with some functional representation of booleans. For example, with a common church encoding:
and now you can consider a test (that returns one of these encoded booleans) as a function that accepts a "success" continuation and a "failure" one, and uses that to continue:
If you want to experiment with this, you need to consider the fact that Scheme is not a lazy language, so you need to thunk things up. For example:
(But you still need to revise existing predicates etc to return these booleans.)
Either way,
call/cc
in itself isn't really doing anything relevant...您可以仅使用高阶过程来实现 if。这是明显的非柯里化的 Church 编码:
你根本不需要延续。 True 是一个执行其第一个参数的二元函数; False 是一个执行第二个参数的二元函数。 If 是一个三元函数,它通过测试 (?) 确定 True/False 来将它们排序在一起,并为其提供延迟结果的两个函数。
You can implement if with only higher-order procedures. This is the obvious uncurried Church encoding:
You don't need continuations at all. True is a binary function that executes it's first argument; False is a binary function that executes it's second argument. If is a ternary function that sequences them together by getting the True/False determined by the test (?), and giving it the two functions that delay the consequents.