如何返回球拍的连续性?
我正在尝试建立在球拍连续性背后的良好直觉,我想写一个功能 暂停其处决,并返回一个延续,当称为继续工作的工作时。
我用这个代码实现了这一点
# 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我在尝试另一个示例后发现了答案,我不完全理解为什么,但是
k
的值成为第一个呼叫之后的2Okay 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您已经暂停了定义内的计算 - 它是“等待”
x
。的值
(在x ... 。)
插图:
当您执行
(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 calledk
in(define x ...
.)Illustration:
When you do
(k 1)
, the definition continues withx
bound to1
, then it prints the output, and lastly binds the value of(+ x 1)
tok
.