如何返回球拍的连续性?

发布于 2025-01-25 20:49:28 字数 1754 浏览 3 评论 0原文

我正在尝试建立在球拍连续性背后的良好直觉,我想写一个功能 暂停其处决,并返回一个延续,当称为继续工作的工作时。

我用这个代码实现了这一点

# lang racket

(define resume-with null) ;; called to resume a call of suspend
(define (suspend abort)
  (call/cc (lambda (k)
             (set! resume-with k)
             (abort))))



(define (foo) ;; a function that do suspended work
  (call/cc (lambda (abort)
             ;; abort is used to abort from foo from within suspend
             ;; if we do not abort the whole body will be executed
             (printf "-> A\n")
             (define x (suspend abort))
             (printf "-> B\n")
             (define y (suspend abort))
             (printf "-> C\n")
             (+ x y))))

,但是在此示例中,我将延续保存在resume>的中,我想将其返回。

我正在尝试这样的事情,

(define (foo1)
  (call/cc (lambda (abort)
             (define x (call/cc (lambda (k)
                                  (abort k))))
             (printf "x -> ~a\n" x)
             (+ x 1))))

我想仅使用call/cc而无需mustability即可实现单个函数中较小,更简单的示例。

这个示例有效的作品,但似乎最终结果在另一个计算中不可用,我不明白发生了什么

foo.rkt> (define k (foo1)) ;; I call it and save the continuation, so far so good
foo.rkt> k ;; the continuation is a procedure
#<procedure>
foo.rkt> (k 1) ;; I call the continuation, the print shows up so we seem to be in good shape, but
x -> 1
foo.rkt> (+ 1 (k 1)) ;; if I try to apply this inside another computation I get this weird error, the 2 in the error message suggests is some what computed the `(+ x 1)` but the rest of the error I don't know how to interpret 
; application: not a procedure;
;  expected a procedure that can be applied to arguments
;   given: 2

I'm trying to build a good intuition behind continuations in Racket, I want to write a function that
suspend it's execution and returns a continuation that when called continue the work from where it was suspended.

I kind of achieved this with this code

# lang racket

(define resume-with null) ;; called to resume a call of suspend
(define (suspend abort)
  (call/cc (lambda (k)
             (set! resume-with k)
             (abort))))



(define (foo) ;; a function that do suspended work
  (call/cc (lambda (abort)
             ;; abort is used to abort from foo from within suspend
             ;; if we do not abort the whole body will be executed
             (printf "-> A\n")
             (define x (suspend abort))
             (printf "-> B\n")
             (define y (suspend abort))
             (printf "-> C\n")
             (+ x y))))

But in this example I save the continuation in resume-with I want to return it instead.

I was trying something like this

(define (foo1)
  (call/cc (lambda (abort)
             (define x (call/cc (lambda (k)
                                  (abort k))))
             (printf "x -> ~a\n" x)
             (+ x 1))))

I would like to achieve the smaller and simpler example in a single function, using only call/cc and no mutability.

This example kind of works, but it seems that the final result is not usable in another computation, I don't understand what's happening

foo.rkt> (define k (foo1)) ;; I call it and save the continuation, so far so good
foo.rkt> k ;; the continuation is a procedure
#<procedure>
foo.rkt> (k 1) ;; I call the continuation, the print shows up so we seem to be in good shape, but
x -> 1
foo.rkt> (+ 1 (k 1)) ;; if I try to apply this inside another computation I get this weird error, the 2 in the error message suggests is some what computed the `(+ x 1)` but the rest of the error I don't know how to interpret 
; application: not a procedure;
;  expected a procedure that can be applied to arguments
;   given: 2

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

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

发布评论

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

评论(2

明月夜 2025-02-01 20:49:28

好吧,我在尝试另一个示例后发现了答案,我不完全理解为什么,但是k的值成为第一个呼叫之后的2

foo.rkt> (define k (foo2))
foo.rkt> (k 1)
x -> 1
foo.rkt> k
2
foo.rkt> (+ 1 k)
3
foo.rkt> 

Okay I find out after trying another example, I don't fully understand why but the value of k becomes the 2 after the first call

foo.rkt> (define k (foo2))
foo.rkt> (k 1)
x -> 1
foo.rkt> k
2
foo.rkt> (+ 1 k)
3
foo.rkt> 
初熏 2025-02-01 20:49:28

您已经暂停了定义内的计算 - 它是“等待” x
的值
(在x ... 。)

插图:

(define (foo1)
  (call/cc (lambda (abort)
             (define x {this is where the continuation returns})
             (+ x 1))))

当您执行(K 1)时,定义以x绑定到1,然后它打印输出,最后将(+ x 1)的值绑定到k

You have suspended the computation inside the definition - it is "waiting" for the value of x.
(The initial "value" of k is not the value returned from (foo1) but the continuation you called k in (define x ....)

Illustration:

(define (foo1)
  (call/cc (lambda (abort)
             (define x {this is where the continuation returns})
             (+ x 1))))

When you do (k 1), the definition continues with x bound to 1, then it prints the output, and lastly binds the value of (+ x 1) to k.

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