提前退出并换班和重置



尝试用Guile方案理解定界连续

多亏了这个视频(很棒(,我成功地掌握了香草味的conts(call/cc(

现在我想转到定界的conts

我有一个用call/cc 提前退出的最小例子

(define (my-early-exit)
(let ((
my-val (call/cc
(lambda (the-continuation)
(display "this will be executed")
(display "n")
(the-continuation 5) ;;early exit
(display "this will not be executed")))))
(display my-val)))

好的,我可以运行这个,我理解它的作用

如何使用移位和重置来编写与此代码等效的代码?

我对shiftreset感到困惑,但根据我不久前的一些笔记,我就是这样理解的。我欢迎比我更了解这一点的澄清和/或更正人员。

类似的东西,或多或少,将是

(define (mee/sr)
(reset
(display "this will happenn")
(shift k 5)
(display "this won't happen, unless you call kn")))

所以,这里(如果我理解的话!(:

  • reset以与call/cc类似的方式建立了一个"你可以到达的地方",只是没有明确的变量
  • (shift k ...)将:
    1. 重置为动态最接近的reset
    2. 使用绑定到延续(?name(的k来评估...,如果调用该延续,将从shift形式返回其参数值,然后继续

所以在这种情况下,我根本不使用k,所以shift之后的形式永远不会发生。但在这种情况下:

(define (another)
(reset
(display "heren")
(let ((v (shift k (k "and heren"))))
(display v))
(display "and finally here as welln")))

然后

> (another)
here
and here
and finally here as well

为了避免这一切看起来过于明显:

(define (yet-another)
(reset
(display "heren")
(let ((v (shift k
(k "and heren")
(displayln "there is also Sheemishn"))))
(display v))
(display "and finally here as welln")))

然后

> (yet-another)
here
and here
and finally here as well
there is also Sheemish

这里有大量关于shiftreset的信息,尽管我并不完全理解它们。

最新更新