寻求人为的示例代码:延续!

发布于 2024-07-18 00:27:04 字数 502 浏览 7 评论 0原文

所以我相信我现在至少在某种程度上理解了延续,这要归功于 社区计划维基在固定天数内学习方案

但我想要更多的练习——也就是说,我可以在脑海中完成更多的示例代码(最好是人为的,这样就不会出现无关的东西来分散对概念的注意力)。

具体,我想通过恢复和/或协程来解决更多问题,而不是仅仅使用它们来退出循环或其他什么(这相当简单)。

不管怎样,如果你知道除了我上面链接的教程之外的好教程,或者如果你愿意发布你写的一些东西,这将是一个很好的练习,我将非常感激!

So I believe I understand continuations now, at least on some level, thanks to the community scheme wiki and Learn Scheme in Fixnum Days.

But I'd like more practice -- that is, more example code I can work through in my head (preferably contrived, so there's not extraneous stuff to distract from the concept).

Specifically, I'd like to work through more problems with continuations that resume and/or coroutines, as opposed to just using them to exit a loop or whatever (which is fairly straightforward).

Anyway, if you know of good tutorials besides the ones I linked above, or if you'd care to post something you've written that would be a good exercise, I'd be very appreciative!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

别挽留 2024-07-25 00:27:04

是的,继续下去可能会非常令人费解。 这是我不久前发现的一个很好的谜题 - 尝试找出打印的内容和原因:

(define (mondo-bizarro)
  (let ((k (call/cc (lambda (c) c)))) ; A
    (write 1)
    (call/cc (lambda (c) (k c))) ; B 
    (write 2)
    (call/cc (lambda (c) (k c))) ; C
    (write 3)))

(mondo-bizarro)

解释它是如何工作的(包含剧透!):

  1. 第一个 call/cc 存储返回它自己的延续并存储它在k中。
  2. 数字1被写入屏幕。
  3. 当前的延续(将在 B 点继续)返回到 k,k 返回到 A
  4. 这次,k 现在绑定到我们在 B 处获得的延续
  5. 数字 1 再次写入screen
  6. 将在 B 点继续的当前延续返回到 k,这是到另一个点 B 的另一个(但不同)延续
  7. 一旦我们回到原始延续,请务必注意这里 k仍然绑定到 A
  8. 数字2 被写入屏幕
  9. 当前的继续,即在 C 点继续,返回到 k,k 返回到 A
  10. 这次,k 是现在绑定到我们在 C 处获得的延续
  11. 数字 1 再次写入屏幕
  12. 当前的延续(将在 B 点继续)返回到 k,k 返回到 C
  13. 数字 3 被写入屏幕
  14. 就完成了< /a>

因此,正确的输出是11213。 我用粗体文本表示的最常见的症结是 - 重要的是要注意,当您使用延续来“重置” k 的值时,它不会影响原始延续中的 k 值。 一旦你知道了,它就会变得更容易理解。

Yeah, continuations can be pretty mind-bending. Here's a good puzzle I found a while back - try to figure out what's printed and why:

(define (mondo-bizarro)
  (let ((k (call/cc (lambda (c) c)))) ; A
    (write 1)
    (call/cc (lambda (c) (k c))) ; B 
    (write 2)
    (call/cc (lambda (c) (k c))) ; C
    (write 3)))

(mondo-bizarro)

Explanation of how this works (contains spoilers!):

  1. The first call/cc stores returns it's own continuation and stores it in k.
  2. The number 1 is written to the screen.
  3. The current continuation, which is to continue at point B, is returned to k, which returns to A
  4. This time, k is now bound to the continuation we got at B
  5. The number 1 is written again to the screen
  6. The current continuation, which is to continue at point B, is returned to k, which is another (but different) continuation to another point B
  7. Once we're back in the original continuation, it's important to note that here k is still bound to A
  8. The number 2 is written to the screen
  9. The current continuation, which is to continue at point C, is returned to k, which returns to A
  10. This time, k is now bound to the continuation we got at C
  11. The number 1 is written again to the screen
  12. The current continuation, which is to continue at point B, is returned to k, which returns to C
  13. The number 3 is written to the screen
  14. And you're done

Therefore, the correct output is 11213. The most common sticking point I've put in bold text - it's important to note that when you use continuations to 'reset' the value of k that it doesn't affect the value of k back in the original continuation. Once you know that it becomes easier to understand.

弃爱 2024-07-25 00:27:04

布朗大学的编程语言课程有一个关于延续的问题 公开可用。

Brown University's programming languages course has a problem set on continuations publicly available.

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