可以“如果”使用“call/cc”来实现?

发布于 2024-11-27 01:20:03 字数 211 浏览 1 评论 0原文

我被告知“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓味的萝莉 2024-12-04 01:20:03

你不能——你必须有某种方法来测试事物并根据它们是真是假采取行动。不过,您可以通过布尔值的一些函数表示来接近。例如,使用常见的教堂编码:

(define (true x y) x)
(define (false x y) y)

现在您可以将测试(返回这些编码布尔值之一)视为接受“成功”延续和“失败”延续的函数,并使用它来继续:

(define (if c x y) (c x y))

如果您想要尝试这个,你需要考虑这样一个事实:Scheme 不是一种惰性语言,所以你需要把事情搞清楚。例如:(

(define (true x y) (x))
(define (false x y) (y))
(define-syntax if
  [(if c x y) (c (lambda () x) (lambda () y))])

但您仍然需要修改现有谓词等以返回这些布尔值。)

无论哪种方式, 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:

(define (true x y) x)
(define (false x y) y)

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:

(define (if c x y) (c x y))

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:

(define (true x y) (x))
(define (false x y) (y))
(define-syntax if
  [(if c x y) (c (lambda () x) (lambda () y))])

(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...

蛮可爱 2024-12-04 01:20:03

您可以仅使用高阶过程来实现 if。这是明显的非柯里化的 Church 编码:

IF ? T E === (? (lambda () T) (lambda () F))

TRUE     === (lambda (t _) (t))
FALSE    === (lambda (_ f) (f))

你根本不需要延续。 True 是一个执行其第一个参数的二元函数; False 是一个执行第二个参数的二元函数。 If 是一个三元函数,它通过测试 (?) 确定 True/False 来将它们排序在一起,并为其提供延迟结果的两个函数。

You can implement if with only higher-order procedures. This is the obvious uncurried Church encoding:

IF ? T E === (? (lambda () T) (lambda () F))

TRUE     === (lambda (t _) (t))
FALSE    === (lambda (_ f) (f))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文